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(LayoutKind.Sequential)]
public struct SystemTime
{
[MarshalAs(UnmanagedType.U2)]
public short Year;
[MarshalAs(UnmanagedType.U2)]
public short Month;
[MarshalAs(UnmanagedType.U2)]
public short DayOfWeek;
[MarshalAs(UnmanagedType.U2)]
public short Day;
[MarshalAs(UnmanagedType.U2)]
public short Hour;
[MarshalAs(UnmanagedType.U2)]
public short Minute;
[MarshalAs(UnmanagedType.U2)]
public short Second;
[MarshalAs(UnmanagedType.U2)]
public short Milliseconds;
}
[DllImport("kernel32.dll")]
public static extern void GetLocalTime(
out System.SystemTime systemTime);
[DllImport("kernel32.dll")]
public static extern void GetSystemTime(
out System.SystemTime systemTime);
[DllImport("kernel32.dll")]
public static extern bool SetSystemTime(
ref System.SystemTime systemTime);
[DllImport("kernel32.dll")]
public static extern bool SetLocalTime(
ref System.SystemTime 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.SystemTime sysTime;
if( Win32.GetLocalTime(out sysTime) )
Console.WriteLine("{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****************@TK2MSFTNGP09.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.