C#從網路獲取時間更新本機時間
因本機主機板有問題,一斷電就丟失時間,所以想了個方法從網路上獲取最新時間,這樣就不用自己去對時,網路一通,時間就能自動校正。
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);