Get the exact time for a remote server

In C #, how can I request a remote server for its current time?

Similar functionality for

net time \\servername 

but returns a date including seconds.

thanks

+4
c # time
source share
7 answers

Windows Time Service implements NTP. Here is the C # implementation for the NTP client . The Windows GUI used can be found in the Simple Network Time Protocol Client . This is Valer Bokan.

+2
source share

You can use the NetRemoteTOD function.

Example from http://bytes.com/groups/net-c/246234-netremotetod-usage :

 // The pointer. IntPtr pintBuffer = IntPtr.Zero; // Get the time of day. int pintError = NetRemoteTOD(@"\\sony_laptop", ref pintBuffer); // Get the structure. TIME_OF_DAY_INFO pobjInfo = (TIME_OF_DAY_INFO) Marshal.PtrToStructure(pintBuffer, typeof(TIME_OF_DAY_INFO)); // Free the buffer. NetApiBufferFree(pintBuffer); 
+5
source share

You can try to get daytime on port 13:

 System.Net.Sockets.TcpClient t = new System.Net.Sockets.TcpClient ("yourmachineHOST", 13); System.IO.StreamReader rd = new System.IO.StreamReader (t.GetStream ()); Console.WriteLine (rd.ReadToEnd ()); rd.Close(); t.Close(); 
+3
source share

Using the C # NTP client in Reed Copsey (and David Lane), you can get the "now" timestamp (in ms) from the domain controller / NTP server using:

 InternetTime.SNTPClient sntp = new InternetTime.SNTPClient("ntp1.ja.net"); sntp.Connect(false); // true to update local client clock DateTime dt = sntp.DestinationTimestamp.AddMilliseconds(sntp.LocalClockOffset); string timeStampNow = dt.ToString("dd/MM/yyyy HH:mm:ss.fff"); 
+1
source share
0
source share

If you have access to the file system of a remote system with a UNC path (for example, \\remotehost\foo\bar , for example, using Windows Explorer), you can get a remote datetime , even if it is not a Windows system , followed by workaround. Create a dummy file, read its recording time and discard it. It also works for the local host.

 public DateTime filesystemDateTime(string path) { //create temp file string tempFilePath = Path.Combine(path, "lampo.tmp"); using (File.Create(tempFilePath)) { } //read creation time and use it as current source filesystem datetime DateTime dt = new FileInfo(tempFilePath).LastWriteTime; //delete temp file File.Delete(tempFilePath); return dt; } 
0
source share
 class RemoteSystemTime { static void Main(string[] args) { try { string machineName = "vista-pc"; System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.FileName = "net"; proc.StartInfo.Arguments = @"time \\" + machineName; proc.Start(); proc.WaitForExit(); List<string> results = new List<string>(); while (!proc.StandardOutput.EndOfStream) { string currentline = proc.StandardOutput.ReadLine(); if (!string.IsNullOrEmpty(currentline)) { results.Add(currentline); } } string currentTime = string.Empty; if (results.Count > 0 && results[0].ToLower().StartsWith(@"current time at \\" + machineName.ToLower() + " is ")) { currentTime = results[0].Substring((@"current time at \\" + machineName.ToLower() + " is ").Length); Console.WriteLine(DateTime.Parse(currentTime)); Console.ReadLine(); } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.ReadLine(); } } 
0
source share

All Articles