Set time programmatically using C#

前端 未结 5 2264
礼貌的吻别
礼貌的吻别 2020-12-06 18:41

What is the best way to set the time on a remote machine remotely? The machine is running Windows XP and is receiving the new time through a web service call. The goal is

5条回答
  •  眼角桃花
    2020-12-06 19:07

    This is the Win32 API call for setting system time:

    [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; 
     } 
     [DllImport("kernel32.dll", SetLastError=true)] 
    public static extern bool SetSystemTime(ref SYSTEMTIME theDateTime ); 
    

    I'm not exactly sure how you would get the security worked out such that you could execute that function on the client, though.

    You can get a lot more detail on setting system time at PInvoke.

提交回复
热议问题