You will need the P / Invoke SetLocalTime function from the Windows API. Declare this in C #:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] internal static extern bool SetLocalTime(ref SYSTEMTIME lpSystemTime); [StructLayout(LayoutKind.Sequential)] internal struct SYSTEMTIME { public ushort wYear; public ushort wMonth; public ushort wDayOfWeek;
To set the time, you simply initialize an instance of the SYSTEMTIME structure with the appropriate values ββand call the function. Code example:
SYSTEMTIME time = new SYSTEMTIME(); time.wDay = 1; time.wMonth = 5; time.wYear = 2011; time.wHour = 12; time.wMinute = 15; if (!SetLocalTime(ref time)) {
However, note that the calling process must have the appropriate privileges to call this function. In Windows Vista and later, this means that you will need to query the height of the process.
Alternatively, you can use the SetSystemTime function, which allows you to set the time in UTC (Coordinated Universal Time). The same SYSTEMTIME structure is SYSTEMTIME , and the two functions are called the same way.
Cody gray
source share