现在的位置: 首页 > 综合 > 正文

C#从网络获取时间更新本机时间

2013年12月04日 ⁄ 综合 ⁄ 共 3092字 ⁄ 字号 评论关闭

因本机主板有问题,一断电就丢失时间,所以想了个方法从网络上获取最新时间,这样就不用自己去对时,网络一通,时间就能自动校正。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

namespace AutoExitWindows
{
 /// <summary>
 /// ReSetLocalTimeFromNetwork 的摘要说明。
 /// </summary>
 public class ReSetLocalTimeFromNetwork
 {
  [DllImport("kernel32",SetLastError=true)]
   static extern int SetSystemTime(ref SYSTEMTIME lpSystemTime);
  [DllImport("kernel32",SetLastError=true)]
   static extern int GetLastError();
  //Public Declare Function GetLastError Lib "kernel32" Alias "GetLastError" () As Long
  //Public Declare Function SetSystemTime Lib "kernel32" Alias "SetSystemTime" (lpSystemTime As SYSTEMTIME) As Long
  public struct SYSTEMTIME
  {
   short wYear;
   short wMonth;
   short wDayOfWeek;
   short wDay;
   short wHour;
   short wMinute;
   short wSecond;
   short wMilliseconds;
   
   public SYSTEMTIME(short year, short month, short dayofweek, short day, short hour, short minute, short second, short milliseconds)
   {
    wYear=year;
    wMonth=month;
    wDayOfWeek=dayofweek;
    wDay=day;
    wHour=hour;
    wMinute=minute;
    wSecond=second;
    wMilliseconds=milliseconds;
   }
  }

  public ReSetLocalTimeFromNetwork(string Server, string Port, string TimeZone)
  {
   try
   {
    //取网络时间
    SNTPTimeClient client=new SNTPTimeClient(Server,Port);
    client.Connect();    
    string strTest=client.ToString();
    Console.Write(strTest);

    //取出年、月、日、时、分、秒、微秒
    short intYear, intMonth, intDayofWeek, intDay, intHour, intMinute, intSecond, intMilliseconds;    

    Char[] split=new Char[1];
    split[0]=Convert.ToChar("/n");
    string[] temp=strTest.Split(split,100);
    
    string strDate="";
    int intPos=-1;
    for(int i=0; i<temp.Length-1; i++)
    {
     strDate=temp[i];
     if(strDate.Substring(0,10)=="Local time")
     {
      intPos=1;
      break;
     }
     else
      Console.Write(strDate.Substring(0,10) + "/n");
    }
    if (intPos<0 )
    {
     throw new Exception("Can't get server time!");     
    }
    strDate=strDate.Substring(11);

    //设置本地时间
    //注意,设置时,写入的时候应该是格林威治时间
    DateTime dtmDate=Convert.ToDateTime(strDate);
    dtmDate=dtmDate.AddHours((-1) * Convert.ToInt16(TimeZone));
    intYear=Convert.ToInt16 (dtmDate.Year);
    intMonth=Convert.ToInt16 (dtmDate.Month);
    intDayofWeek=Convert.ToInt16 (dtmDate.DayOfWeek);
    intDay=Convert.ToInt16 (dtmDate.Day);
    intHour=Convert.ToInt16 (dtmDate.Hour);
    intMinute=Convert.ToInt16 (dtmDate.Minute);
    intSecond=Convert.ToInt16 (dtmDate.Second);
    intMilliseconds=Convert.ToInt16 (dtmDate.Millisecond );

    SYSTEMTIME systime=new SYSTEMTIME(intYear, intMonth, intDayofWeek, intDay, intHour, intMinute, intSecond, intMilliseconds);    
    int intValue=SetSystemTime(ref systime);  
    if (intValue==0)
    {
     intValue=GetLastError();
     throw new Exception(intValue.ToString() + " error, can't set local time.");
    }
    //MessageBox.Show(intValue.ToString() + "/n" + DateTime.Now.ToString() + "/n" + strDate);    
   }
   catch(Exception e1)
   {
    throw e1;
   }
  }

 }
}

调用时:
ReSetLocalTimeFromNetwork objResetTime=new ReSetLocalTimeFromNetwork(strRemoteIP,strRemotePort,strTimeZone);

 

抱歉!评论已关闭.