lamp

Set System Time in C#

Published 17 August 07 10:10 AM | Frederick.Chapleau

Declarations: 

[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 SystemTime systemTime);

[DllImport("kernel32.dll")]

public static extern void GetSystemTime(

out SystemTime systemTime);

[DllImport("kernel32.dll")]

public static extern bool SetSystemTime(

ref SystemTime systemTime);

[DllImport("kernel32.dll")]

public static extern bool SetLocalTime(

ref SystemTime systemTime);

Usage :

SystemTime t;

GetLocalTime(out t);

// do something

SetSystemTime(ref t);

Filed under:

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Protected by FormShield
Refresh
Listen
Please enter the characters shown on the image


Code: