当前位置:编程学习 > C#/ASP.NET >>

C# 网络时间校准

请高手解决一下:C# 网络时间校准问题。

WinForm程序,要求把系统时间与网络时间调整一样。

希望能有代码。

谢谢

补充:错误 1 :找不到类型或命名空间名称“SNTPTimeClient”(是否缺少 using 指令或程序集引用?)
追问:调用时,参数(strRemoteIP,strRemotePort,strTimeZone)里填哪些数据,能举个例子么?谢

答案: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);

上一个:C# sda.Update()
下一个:关于C#集合,索引器,迭代器

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,