Re: Set the local time
From: Anders Norås [MCAD] (anders.noras_at_objectware.no)
Date: 12/16/04
- Next message: freddy: "nt authentication"
- Previous message: Stephen Brooker: "Re: Email field on a WinForm"
- In reply to: Drunkalot: "Set the local time"
- Next in thread: Drunkalot: "Re: Set the local time"
- Reply: Drunkalot: "Re: Set the local time"
- Reply: Drunkalot: "Re: Set the local time"
- Messages sorted by: [ date ] [ thread ]
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/
- Next message: freddy: "nt authentication"
- Previous message: Stephen Brooker: "Re: Email field on a WinForm"
- In reply to: Drunkalot: "Set the local time"
- Next in thread: Drunkalot: "Re: Set the local time"
- Reply: Drunkalot: "Re: Set the local time"
- Reply: Drunkalot: "Re: Set the local time"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|