How do I set XP System Time?

  • Thread starter Thread starter Mike Kearl
  • Start date Start date
M

Mike Kearl

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.
 
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,

Please note the error I provided in my previous code. All references
to System.SystemTime in the API's should be changed to Win32.SystemTime

//Andreas

Andreas Håkansson said:
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 said:
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.
 
AHHH.. I was just trying to find the namespace for that and couldn't find
it. that helps a ton :)

I appreciate your help


Andreas Håkansson said:
Mike,

Please note the error I provided in my previous code. All references
to System.SystemTime in the API's should be changed to Win32.SystemTime

//Andreas

Andreas Håkansson said:
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 said:
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.
 
ok I got the api's working so that I can read the time but I can't get it to
set. I have admin rights and keep getting an error.. Do you have any code
that would set it so that I can compare?

Mike.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top