以编程方式更改系统date

我如何用C#编程改变本地系统的date和时间?

这是我find答案的地方。

为了提高清晰度,我在这里转发了它。

定义这个结构:

[StructLayout(LayoutKind.Sequential)] 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; } 

将下面的extern方法添加到你的类中:

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

然后用你的结构体的一个实例调用这个方法:

 SYSTEMTIME st = new SYSTEMTIME(); st.wYear = 2009; // must be short st.wMonth = 1; st.wDay = 1; st.wHour = 0; st.wMinute = 0; st.wSecond = 0; SetSystemTime(ref st); // invoke this method. 

您可以使用一个DOS命令的调用,但在Windows DLL中调用该函数是一个更好的方法来做到这一点。

 public struct SystemTime { public ushort Year; public ushort Month; public ushort DayOfWeek; public ushort Day; public ushort Hour; public ushort Minute; public ushort Second; public ushort Millisecond; }; [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)] public extern static void Win32GetSystemTime(ref SystemTime sysTime); [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)] public extern static bool Win32SetSystemTime(ref SystemTime sysTime); private void button1_Click(object sender, EventArgs e) { // Set system date and time SystemTime updatedTime = new SystemTime(); updatedTime.Year = (ushort)2009; updatedTime.Month = (ushort)3; updatedTime.Day = (ushort)16; updatedTime.Hour = (ushort)10; updatedTime.Minute = (ushort)0; updatedTime.Second = (ushort)0; // Call the unmanaged function that sets the new date and time instantly Win32SetSystemTime(ref updatedTime); } 
  1. PInvoke调用Win32 API SetSystemTime,( 示例
  2. System.Management类与WMI类Win32_OperatingSystem并在该类上调用SetDateTime。

两者都要求调用者已被授予SeSystemTimePrivilege,并且此权限已启用。

很多伟大的观点和方法已经在这里,但是这里有一些目前被遗漏的规范,我觉得可能会绊倒一些人,让人混淆。

  1. Windows Vista,7,8操作系统上,这将需要一个UAC提示,以便获得成功执行SetSystemTime函数所需的pipe理权限。 原因是调用进程需要SE_SYSTEMTIME_NAME特权。
  2. SetSystemTime函数期望协调世界时(UTC)中SYSTEMTIME结构。 否则将无法正常工作。

根据您获取DateTime值的位置/方式,在SYSTEMTIME结构中设置相应的值之前,最好先安全地使用ToUniversalTime()

代码示例:

 DateTime tempDateTime = GetDateTimeFromSomeService(); DateTime dateTime = tempDateTime.ToUniversalTime(); SYSTEMTIME st = new SYSTEMTIME(); // All of these must be short st.wYear = (short)dateTime.Year; st.wMonth = (short)dateTime.Month; st.wDay = (short)dateTime.Day; st.wHour = (short)dateTime.Hour; st.wMinute = (short)dateTime.Minute; st.wSecond = (short)dateTime.Second; // invoke the SetSystemTime method now SetSystemTime(ref st); 

由于我在评论中提到它,下面是一个C ++ / CLI包装器:

 #include <windows.h> namespace JDanielSmith { public ref class Utilities abstract sealed /* abstract sealed = static */ { public: CA_SUPPRESS_MESSAGE("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands") static void SetSystemTime(System::DateTime dateTime) { LARGE_INTEGER largeInteger; largeInteger.QuadPart = dateTime.ToFileTimeUtc(); // "If your compiler has built-in support for 64-bit integers, use the QuadPart member to store the 64-bit integer." FILETIME fileTime; // "...copy the LowPart and HighPart members [of LARGE_INTEGER] into the FILETIME structure." fileTime.dwHighDateTime = largeInteger.HighPart; fileTime.dwLowDateTime = largeInteger.LowPart; SYSTEMTIME systemTime; if (FileTimeToSystemTime(&fileTime, &systemTime)) { if (::SetSystemTime(&systemTime)) return; } HRESULT hr = HRESULT_FROM_WIN32(GetLastError()); throw System::Runtime::InteropServices::Marshal::GetExceptionForHR(hr); } }; } 

C#客户端代码现在非常简单:

 JDanielSmith.Utilities.SetSystemTime(DateTime.Now); 

使用此function更改系统的时间(在窗口8中testing)

  void setDate(string dateInYourSystemFormat) { var proc = new System.Diagnostics.ProcessStartInfo(); proc.UseShellExecute = true; proc.WorkingDirectory = @"C:\Windows\System32"; proc.CreateNoWindow = true; proc.FileName = @"C:\Windows\System32\cmd.exe"; proc.Verb = "runas"; proc.Arguments = "/C date " + dateInYourSystemFormat; try { System.Diagnostics.Process.Start(proc); } catch { MessageBox.Show("Error to change time of your system"); Application.ExitThread(); } } void setTime(string timeInYourSystemFormat) { var proc = new System.Diagnostics.ProcessStartInfo(); proc.UseShellExecute = true; proc.WorkingDirectory = @"C:\Windows\System32"; proc.CreateNoWindow = true; proc.FileName = @"C:\Windows\System32\cmd.exe"; proc.Verb = "runas"; proc.Arguments = "/C time " + timeInYourSystemFormat; try { System.Diagnostics.Process.Start(proc); } catch { MessageBox.Show("Error to change time of your system"); Application.ExitThread(); } } 

示例: 调用窗体 setDate的加载方法 (“5-6-92”); setTime(“2:4:5 AM”);

proc.Arguments =“/ C Date:”+ dateInYourSystemFormat;

这是function:

 void setDate(string dateInYourSystemFormat) { var proc = new System.Diagnostics.ProcessStartInfo(); proc.UseShellExecute = true; proc.WorkingDirectory = @"C:\Windows\System32"; proc.CreateNoWindow = true; proc.FileName = @"C:\Windows\System32\cmd.exe"; proc.Verb = "runas"; proc.Arguments = "/C Date:" + dateInYourSystemFormat; try { System.Diagnostics.Process.Start(proc); } catch { MessageBox.Show("Error to change time of your system"); Application.ExitThread(); } }