SoFunction
Updated on 2025-03-06

Detailed explanation of the method example of C# synchronizing network time

This article describes the method of synchronizing network time in C#. Share it for your reference. The specific analysis is as follows:

The system time of the customer's machine often makes mistakes, which makes the software they made cannot be used normally, so a small function of synchronizing network time was added. It is simple to implement, but it is very useful.

This small function is to first obtain the network time, and then modify the system time to the time obtained from the network. The following are the specific implementations:

Get network time:

using System; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ; 
using ;
using ; 
 /// <summary> 
 /// Network time /// </summary> 
 public class NetTime
 {
  /// <summary> 
  /// Get the standard Beijing time, read/  /// </summary> 
  /// <returns>Return to network time</returns>  public DateTime GetBeijingTime()
  {
   DateTime dt;
   WebRequest wrt = null;
   WebResponse wrp = null;
   try
   {
    wrt = ("/");
    wrp = ();
    string html = ;
    using (Stream stream = ())
    {
     using (StreamReader sr = new StreamReader(stream,Encoding.UTF8))
     {
      html = ();
     }
    }
    string[] tempArray = (';');
    for (int i = 0; i &lt; ; i++)
    {
     tempArray[i] = tempArray[i].Replace("\r\n", "");
    }
    string year = tempArray[1].Split('=')[1];
    string month = tempArray[2].Split('=')[1];
    string day = tempArray[3].Split('=')[1];
    string hour = tempArray[5].Split('=')[1];
    string minite = tempArray[6].Split('=')[1];
    string second = tempArray[7].Split('=')[1];
    dt = (year + "-" + month + "-" + day + " " + hour + ":" + minite + ":" + second);
   }
   catch (WebException)
   {
    return ("2011-1-1");
   }
   catch (Exception)
   {
    return ("2011-1-1");
   }
   finally
   {
    if (wrp != null)
     ();
    if (wrt != null)
     ();
   }
   return dt;
  }
}

Get the network time, return a DateTime object, and then pass it to the method of setting the system time, and modify the system time.

Synchronize system time:

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ; 
 /// &lt;summary&gt;
 /// Update system time /// &lt;/summary&gt;
 public class UpdateTime
 {
  //Set the API function for system time  [DllImport("")]
  private static extern bool SetLocalTime(ref SYSTEMTIME time);
  [StructLayout()]
  private struct SYSTEMTIME
  {
   public short year;
   public short month;
   public short dayOfWeek;
   public short day;
   public short hour;
   public short minute;
   public short second;
   public short milliseconds;
  }
  /// &lt;summary&gt;
  /// Set system time  /// &lt;/summary&gt;
  /// <param name="dt">Time required</param>  /// <returns> Return to the system time setting status, true is successful, false is failure</returns>  public static bool SetDate(DateTime dt)
  {
   SYSTEMTIME st;
    = (short);
    = (short);
    = (short);
    = (short);
    = (short);
    = (short);
    = (short);
    = (short);
   bool rt = SetLocalTime(ref st);
   return rt;
  }
}

The two methods are written in two classes respectively. You only need to instantiate two objects on the client and then call their methods in turn. It is simple and practical.

I hope this article will be helpful to everyone's C# programming.