none
Change Windows Time & Date RRS feed

Answers

  • You can P/Invoke the SetLocalTime API.  Here is a wrapper method (SetTime) for calling that API.

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]  
    private struct SYSTEMTIME  
    {  
        public ushort wYear;  
        public ushort wMonth;  
        public ushort wDayOfWeek;  
        public ushort wDay;  
        public ushort wHour;  
        public ushort wMinute;  
        public ushort wSecond;  
        public ushort wMilliseconds;  
    }  
     
    [System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", SetLastError=true)]  
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]  
    private static extern bool SetLocalTime([System.Runtime.InteropServices.InAttribute()] ref SYSTEMTIME lpSystemTime);  
     
    [System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", SetLastError=true)]  
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]  
    private static extern bool SetSystemTime([System.Runtime.InteropServices.InAttribute()] ref SYSTEMTIME lpSystemTime);  
     
    private static void SetTime(DateTime dt)  
    {  
        SYSTEMTIME st;  
        st.wYear = (ushort)dt.Year;  
        st.wMonth = (ushort)dt.Month;  
        st.wDayOfWeek = (ushort)0; // Not used by SetLocalTime  
        st.wDay = (ushort)dt.Day;  
        st.wHour = (ushort)dt.Hour;  
        st.wMinute = (ushort)dt.Minute;  
        st.wSecond = (ushort)dt.Second;  
        st.wMilliseconds = (ushort)dt.Millisecond;  
     
        if (dt.Kind == DateTimeKind.Utc)  
        {  
            if (!SetSystemTime(ref st))  
                throw new Win32Exception();  
        }  
        else 
        {  
            if (!SetLocalTime(ref st))  
                throw new Win32Exception();  
        }  
    }  
     
    private void Test()  
    {  
        DateTime d = new DateTime(2008, 1, 1, 21, 0, 0); // July 1, 2008 9:00PM  
        SetTime(d);  
    • Edited by Jason Kresowaty Wednesday, July 2, 2008 1:09 AM Add SetLastError=true.
    • Marked as answer by jack 321 Friday, July 4, 2008 2:37 AM
    Wednesday, July 2, 2008 1:08 AM

All replies

  • You can P/Invoke the SetLocalTime API.  Here is a wrapper method (SetTime) for calling that API.

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]  
    private struct SYSTEMTIME  
    {  
        public ushort wYear;  
        public ushort wMonth;  
        public ushort wDayOfWeek;  
        public ushort wDay;  
        public ushort wHour;  
        public ushort wMinute;  
        public ushort wSecond;  
        public ushort wMilliseconds;  
    }  
     
    [System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", SetLastError=true)]  
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]  
    private static extern bool SetLocalTime([System.Runtime.InteropServices.InAttribute()] ref SYSTEMTIME lpSystemTime);  
     
    [System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", SetLastError=true)]  
    [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]  
    private static extern bool SetSystemTime([System.Runtime.InteropServices.InAttribute()] ref SYSTEMTIME lpSystemTime);  
     
    private static void SetTime(DateTime dt)  
    {  
        SYSTEMTIME st;  
        st.wYear = (ushort)dt.Year;  
        st.wMonth = (ushort)dt.Month;  
        st.wDayOfWeek = (ushort)0; // Not used by SetLocalTime  
        st.wDay = (ushort)dt.Day;  
        st.wHour = (ushort)dt.Hour;  
        st.wMinute = (ushort)dt.Minute;  
        st.wSecond = (ushort)dt.Second;  
        st.wMilliseconds = (ushort)dt.Millisecond;  
     
        if (dt.Kind == DateTimeKind.Utc)  
        {  
            if (!SetSystemTime(ref st))  
                throw new Win32Exception();  
        }  
        else 
        {  
            if (!SetLocalTime(ref st))  
                throw new Win32Exception();  
        }  
    }  
     
    private void Test()  
    {  
        DateTime d = new DateTime(2008, 1, 1, 21, 0, 0); // July 1, 2008 9:00PM  
        SetTime(d);  
    • Edited by Jason Kresowaty Wednesday, July 2, 2008 1:09 AM Add SetLastError=true.
    • Marked as answer by jack 321 Friday, July 4, 2008 2:37 AM
    Wednesday, July 2, 2008 1:08 AM
  • Thanks!
    Ya Mahdi!
    Saturday, July 5, 2008 9:56 AM