Click here to Skip to main content
16,021,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hai.......

I want to change system date using c#.I got code from web,but its not working properly.Its changing system time but thats not correct. is there any other chance to change system time.

My code is

C#
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
    public short wYear;
    public short wMonth;
    public short wDayOfWeek;
    public short wDay;
    public short wHour;
    public short wMinute;
    public short wSecond;
    public short wMilliseconds;
}

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool SetSystemTime(ref SYSTEMTIME theDateTime);

SYSTEMTIME st    = new SYSTEMTIME();
st.wDay          = (short)DateTime.Now.Day;
st.wDayOfWeek    = (short)DateTime.Now.DayOfWeek;
st.wHour         = (short)(DateTime.Now.Hour+6);
st.wMilliseconds = (short)DateTime.Now.Millisecond;
st.wMinute       = (short)(DateTime.Now.Minute);
st.wMonth        = (short)DateTime.Now.Month;
st.wSecond       = (short)DateTime.Now.Second;
st.wYear         = (short)(DateTime.Now.Year);
bool result      = SetSystemTime(ref st); //returns true for me
Posted
Updated 20-Jan-11 23:33pm
v2
Comments
Hemraj Gujar 21-Jan-11 5:37am    
Hi,

I found one article, hope it will be of help

http://www.longhorncorner.com/Forums/ShowMessages.aspx?ThreadID=73042

Hi,

this code is working for me:
C#
[DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
public extern static bool Win32SetSystemTime(ref SystemTime sysTime);

private void button1_Click(object sender, EventArgs e)
{
  // Set system date and time
  SystemTime updatedTime = new SystemTime();
  updatedTime.Year = (ushort)2008;
  updatedTime.Month = (ushort)4;
  updatedTime.Day = (ushort)23;
 // UTC time; it will be modified according to the regional settings of the target computer so the actual hour might differ
  updatedTime.Hour = (ushort)10;
  updatedTime.Minute = (ushort)0;
  updatedTime.Second = (ushort)0;
  // Call the unmanaged function that sets the new date and time instantly
  Win32SetSystemTime(ref updatedTime);
}
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 21-Jan-11 13:58pm    
Very good - my 5.
A like you use EntryPoint on attribute. Many rely on default (same as method name) which is less supportable.
One other comment - instead of calling DateTime.Now for every property, call it once and then use that value:

C#
DateTime now           = DateTime.Now;
SystemTime updatedTime = new SystemTime();
updateTime.Year        = (ushort)(now.Year);
updateTime.Month       = (ushort)(now.Month);
...etc
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Jan-11 13:56pm    
Important fix - my 5.
You should have said re-calling Now is a bug, not just a sloppiness; would mess up results.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900