Re: Set the local time

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Anders Norås [MCAD] (anders.noras_at_objectware.no)
Date: 12/16/04


Date: Thu, 16 Dec 2004 01:48:46 +0100


> How can I set the system time using a DateTime object???
You can't set the local time with the DateTime structure, but you can do it
with the SetSystemTime method in kernel32.dll via PInvoke. The SetSystemTime
method accepts a SystemTime structure as its parameter. Populate this
structure with the corresponding members of the DateTime structure.

public class MyClass
{
 public static void Main()
 {
  DateTime dt=DateTime.Now;
  SystemTime st=new SystemTime();
  st.Year=Convert.ToUInt16(dt.Year);
  st.Hour=Convert.ToUInt16(dt.Hour-1);
  SetSystemTime(ref st);
 }

 [DllImport("kernel32.dll")]
 public static extern int SetSystemTime(ref SystemTime systemTime);
}
[StructLayout(LayoutKind.Sequential)]
public struct SystemTime {
 public System.UInt16 Year;
 public System.UInt16 Month;
 public System.UInt16 DayOfWeek;
 public System.UInt16 Day;
 public System.UInt16 Hour;
 public System.UInt16 Minute;
 public System.UInt16 Second;
 public System.UInt16 Millisecond;
}

Anders Norås
http://dotnetjunkies.com/weblog/anoras/



Relevant Pages

  • Re: Set the local time
    ... had offered an equivalent to SetSystemTime, just as it did for GetSystemTime ... > with the SetSystemTime method in kernel32.dll via PInvoke. ... > method accepts a SystemTime structure as its parameter. ... > structure with the corresponding members of the DateTime structure. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Set the local time
    ... >> How can I set the system time using a DateTime object??? ... > with the SetSystemTime method in kernel32.dll via PInvoke. ... > method accepts a SystemTime structure as its parameter. ...
    (microsoft.public.dotnet.languages.csharp)
  • Should programs handle system time jumps?
    ... handle jumps in the system time. ... The jumps occur when, for demonstrating ... The problem is that certain threads that poll things wait between polls for ... If, in that waitcycle, the systemtime is rewound by a year, ...
    (comp.programming)
  • Re: Set the local time
    ... >>> How can I set the system time using a DateTime object??? ... >> with the SetSystemTime method in kernel32.dll via PInvoke. ... >> method accepts a SystemTime structure as its parameter. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Set the local time
    ... Whats happening??? ... >> How can I set the system time using a DateTime object??? ... > with the SetSystemTime method in kernel32.dll via PInvoke. ... > method accepts a SystemTime structure as its parameter. ...
    (microsoft.public.dotnet.languages.csharp)