Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set time programmatically using C#

Tags:

c#

time

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 to keep the remote machines in synch with the server. The system is locked down so that our web service is the only access, so I cannot use a time server on each remote machine.

like image 459
Jim Blake Avatar asked Oct 15 '08 14:10

Jim Blake


1 Answers

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.

like image 156
Robert S. Avatar answered Oct 23 '22 09:10

Robert S.