need to chanmge windows system time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dviry
    New Member
    • Nov 2009
    • 1

    need to chanmge windows system time

    hello ,

    i need to do some things with windows system time .
    the problem is that i need to change windows system time - and i dont know
    how to change it .

    datetime.new - can't get any parameters ..

    i would like to get some help ..

    thanks
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    A DateTime data type is not for the System time. It can be any DateTime you might be working with: An appointment might have a DateTime. A birthday. The date and time a file was created. Etc.

    The Windows SystemTime is actually a structure you set. There is no .NET Framework feature for this, so you have to do it through a WinAPI call.
    A quick Google for "C# Set Windows time" got these two examples:
    Code:
    [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 );
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
     
    namespace Sample
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            public struct SystemTime
            {
                public ushort Year;
                public ushort Month;
                public ushort DayOfWeek;
                public ushort Day;
                public ushort Hour;
                public ushort Minute;
                public ushort Second;
                public ushort Millisecond;
            };
     
            [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
            public extern static void Win32GetSystemTime(ref SystemTime sysTime);
     
            [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);
     
                // Retrieve the current system date and time
                SystemTime currTime = new SystemTime();
                Win32GetSystemTime(ref currTime);
                // You can now use the struct to retrieve the date and time
                MessageBox.Show("It's " + currTime.Hour + " o'clock. Do you know where your C# code is?");
            }
        }
    }

    Comment

    Working...