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);

 

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值