-3

I have used the below program to change the system date time.

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)
{`enter code here`
    // Set system date and time
    SystemTime updatedTime = new SystemTime();
    updatedTime.Year = (ushort)2009;
    updatedTime.Month = (ushort)3;
    updatedTime.Day = (ushort)16;
    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);
}

System Date is changed but time is not change. My task is to get the NTP Server time and change the system date and time as of NTP server time.Im getting NTP server date and time & changing Date but im not able to change the time of my system

Ken White
  • 123,280
  • 14
  • 225
  • 444
user2894119
  • 1
  • 1
  • 1
  • "discussion-board" is not an appropriate tag for a programming question related to C#. Please use tags that actually apply to the question you're asking, so that they get properly classified for searching and they get the attention of people who can possibly help you get an answer. Don't just grab random tags for no reason. Thanks. – Ken White Oct 18 '13 at 11:24
  • 1
    possible duplicate of [I can't get SetSystemTime to work in Windows Vista using C# with Interop (P/Invoke)](http://stackoverflow.com/questions/2486125/i-cant-get-setsystemtime-to-work-in-windows-vista-using-c-sharp-with-interop-p) – Ken White Oct 18 '13 at 11:25
  • Isn't syncing to an NTP server built in to Windows? – Hans Kesting Oct 18 '13 at 12:24

1 Answers1

1

You declared SetSystemTime() correctly, but then you forgot to use it properly. You cannot ignore the return value. Fix:

  if (!Win32SetSystemTime(ref updatedTime)) {
      throw new System.ComponentModel.Win32Exception();
  }

You'll now discover the likely cause for the failure from the exception. Several possibilities, but we can guess: changing the clock requires admin privileges and your program isn't likely to have them. You have to ask the user for permission to do this, you do so by embedding a manifest that asks for UAC elevation. This answer shows how to do that.

Just in case you think this is unreasonable: do keep in mind that changing the clock is very disruptive to running programs. Lots of essential services that run in Windows depend on an accurate clock. It isn't going to last long either, Windows periodically contacts a time server to get the clock re-calibrated. You'd have to disable that, ask at superuser.com. The additional problems you'll incur, like files getting written with a bad timestamp, scheduled tasks not running when they should, the web browser complaining about incorrect certificates, your project always rebuilding although you made no changes are yours to deal with however. Don't do this.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • in my progam it is changing but in the system it is not shown.Here one more interesting point is Date is changed and i can see that in my system.im running this in Windows Server 2008. – user2894119 Oct 28 '13 at 05:28
  • ntp server time is recieved as UTC time so my system is taking as utc+5:30.How to consider that ntp server time as local time. – user2894119 Oct 29 '13 at 06:41