如何使用C#更改系统日期和时间

如何使用C#更改系统日期和时间

本文介绍了如何使用C#更改系统日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用C#更改系统日期和时间?

我试过以下代码,但对我不适用..请帮助我





How To Change System Date and Time Using C#?
I have tried following code but didn't works for me..so kindly help me


public struct SYSTEMTIME
{
public short wYear;
       public short wMonth;
       public short wDayOfWeek;
       public short wDay;
       public short wHour;
       public short wMinute;
       public short wSecond;
       public short wMilliseconds;
}

[DllImport("kernel32.dll", SetLastError = true)]
public static extern void SetSystemTime(ref SYSTEMTIME st);

public void SetSystemDateTime(DateTime ServerDateTime)
{
       ServerDateTime = ServerDateTime.AddHours(-5).AddMinutes(-30);
       SYSTEMTIME st = new SYSTEMTIME();
       st.wYear = (short)ServerDateTime.Year; // must be short
st.wMonth = (short)ServerDateTime.Month;
       st.wDay = (short)ServerDateTime.Day;
       st.wHour = (short)ServerDateTime.Hour;
       st.wMinute = (short)ServerDateTime.Minute;
       st.wSecond = (short)ServerDateTime.Second;
       st.wMilliseconds = (short)ServerDateTime.Millisecond;
       SetSystemTime(ref st);
}

推荐答案



这篇关于如何使用C#更改系统日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 20:43