-2

I have a web service running that is returning a time. I want to update my system's time with the time that my service is returning . How can I do this in visual studio using C#?

Edit: Exact duplicate of: Set time programmatically using C#

Community
  • 1
  • 1
user62958
  • 4,669
  • 5
  • 32
  • 35
  • KInd of a duplicate of http://stackoverflow.com/questions/516977/updating-system-time-in-visual-studio-using-netc? – Tjipke Feb 05 '09 at 19:16

2 Answers2

2

Why are you bothering to re-invent the wheel?

Windows already has a built in NTP/time service, that will sync the time with any of the public time servers or a private one that you create.

StingyJack
  • 19,041
  • 10
  • 63
  • 122
1

I would write a wrapper for the windows API SetSystemTime. Something like this:

[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( [In] ref SYSTEMTIME st );

(Please note that I have not tested this, but it should get you started)

Restore the Data Dumps
  • 38,967
  • 12
  • 96
  • 122
Khadaji
  • 2,147
  • 2
  • 17
  • 19