国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#同步網(wǎng)絡時間的方法實例詳解

2020-01-24 01:53:29
字體:
來源:轉載
供稿:網(wǎng)友

本文實例講述了C#同步網(wǎng)絡時間的方法。分享給大家供大家參考。具體分析如下:

客戶的機器的系統(tǒng)時間經(jīng)常出錯,導致給他們做的軟件無法正常使用,所以后來就加了一個同步網(wǎng)絡時間的小功能。實現(xiàn)起來很簡單,但是卻很使用。

這個小功能就是先獲取網(wǎng)絡時間,然后將系統(tǒng)的時間修改成從網(wǎng)絡獲得的時間。下面是具體的實現(xiàn):

獲取網(wǎng)絡時間:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Net; using System.Net.Sockets; using System.Text.RegularExpressions; using System.Runtime.InteropServices;using System.Runtime;  /// <summary>  /// 網(wǎng)絡時間  /// </summary>  public class NetTime {  /// <summary>   /// 獲取標準北京時間,讀取http://www.beijing-time.org/time.asp   /// </summary>   /// <returns>返回網(wǎng)絡時間</returns>   public DateTime GetBeijingTime()  {   DateTime dt;   WebRequest wrt = null;   WebResponse wrp = null;   try   {    wrt = WebRequest.Create("http://www.beijing-time.org/time.asp");    wrp = wrt.GetResponse();    string html = string.Empty;    using (Stream stream = wrp.GetResponseStream())    {     using (StreamReader sr = new StreamReader(stream,Encoding.UTF8))     {      html = sr.ReadToEnd();     }    }    string[] tempArray = html.Split(';');    for (int i = 0; i < tempArray.Length; 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 = DateTime.Parse(year + "-" + month + "-" + day + " " + hour + ":" + minite + ":" + second);   }   catch (WebException)   {    return DateTime.Parse("2011-1-1");   }   catch (Exception)   {    return DateTime.Parse("2011-1-1");   }   finally   {    if (wrp != null)     wrp.Close();    if (wrt != null)     wrt.Abort();   }   return dt;  }}

獲取網(wǎng)絡時間,返回一個DateTime對象,然后傳給設置系統(tǒng)時間的方法,修改系統(tǒng)時間。

同步系統(tǒng)時間:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Net;using System.Net.Sockets;using System.Text.RegularExpressions;using System.Runtime.InteropServices;using System.Runtime;  /// <summary> /// 更新系統(tǒng)時間 /// </summary> public class UpdateTime {  //設置系統(tǒng)時間的API函數(shù)  [DllImport("kernel32.dll")]  private static extern bool SetLocalTime(ref SYSTEMTIME time);  [StructLayout(LayoutKind.Sequential)]  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;  }  /// <summary>  /// 設置系統(tǒng)時間  /// </summary>  /// <param name="dt">需要設置的時間</param>  /// <returns>返回系統(tǒng)時間設置狀態(tài),true為成功,false為失敗</returns>  public static bool SetDate(DateTime dt)  {   SYSTEMTIME st;   st.year = (short)dt.Year;   st.month = (short)dt.Month;   st.dayOfWeek = (short)dt.DayOfWeek;   st.day = (short)dt.Day;   st.hour = (short)dt.Hour;   st.minute = (short)dt.Minute;   st.second = (short)dt.Second;   st.milliseconds = (short)dt.Millisecond;   bool rt = SetLocalTime(ref st);   return rt;  }}

兩個方法分別寫在了兩個類里面,只需要在客戶端實例化兩個對象,然后依次調用其方法即可,簡單實用。

希望本文所述對大家的C#程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 南康市| 湘乡市| 宾阳县| 监利县| 吉林省| 颍上县| 长寿区| 饶河县| 石阡县| 宝坻区| 阿尔山市| 禹州市| 广宁县| 岳阳市| 诸暨市| 新河县| 清涧县| 古丈县| 当涂县| 竹北市| 合作市| 绥宁县| 黄龙县| 黄山市| 剑河县| 黄平县| 宜兰市| 朝阳县| 丹棱县| 吉木萨尔县| 斗六市| 和硕县| 九台市| 沙河市| 蛟河市| 同心县| 贵港市| 神农架林区| 郑州市| 兴隆县| 新乡市|