Mike,
Well you would use the time API's in Win32, just make sure your code
has the right permission to execute unmanaged code.
public class Win32
{
private Win32()
{
}
[StructLayout(La youtKind.Sequen tial)]
public struct SystemTime
{
[MarshalAs(Unman agedType.U2)]
public short Year;
[MarshalAs(Unman agedType.U2)]
public short Month;
[MarshalAs(Unman agedType.U2)]
public short DayOfWeek;
[MarshalAs(Unman agedType.U2)]
public short Day;
[MarshalAs(Unman agedType.U2)]
public short Hour;
[MarshalAs(Unman agedType.U2)]
public short Minute;
[MarshalAs(Unman agedType.U2)]
public short Second;
[MarshalAs(Unman agedType.U2)]
public short Milliseconds;
}
[DllImport("kern el32.dll")]
public static extern void GetLocalTime(
out System.SystemTi me systemTime);
[DllImport("kern el32.dll")]
public static extern void GetSystemTime(
out System.SystemTi me systemTime);
[DllImport("kern el32.dll")]
public static extern bool SetSystemTime(
ref System.SystemTi me systemTime);
[DllImport("kern el32.dll")]
public static extern bool SetLocalTime(
ref System.SystemTi me systemTime);
}
Using these functions you can both set and get the system time. Using the
local time functions, you
get the time with the correct timezone and daylight savingstime. You would
use the GetLocalTime
APi like this:
Win32.SystemTim e sysTime;
if( Win32.GetLocalT ime(out sysTime) )
Console.WriteLi ne("{0}:{1}:{2} ", sysTime.Hour, sysTime.Minute,
sysTime.Second) ;
To set the local time you would create a SystemTime structture, populate it
and pass it using the
ref keywords, instead of the out keyword which GetLocalTime uses, to the
SetLocalTime API.
HTH,
//Andreas
"Mike Kearl" <mk****@hotmail .com> skrev i meddelandet
news:ui******** ********@TK2MSF TNGP09.phx.gbl. ..
I want to set the local Date Time of the xp systems in my organization
using C# and a winform application.
From what I have read this is not an easy task anymore. You have to
obtain privlege and then set it from there.
Does anyone have a good C# example on how to do this? I want to sync my
local system to the time of my sql server. I can get the datetime of the
server but I dont know how to set the local time.
I was thinking I could console out and then run DOS set commands but this
is messy and would like to find a nice clean solution.