C#同步網(wǎng)絡(luò)時(shí)間的方法實(shí)例詳解
本文實(shí)例講述了C#同步網(wǎng)絡(luò)時(shí)間的方法。分享給大家供大家參考。具體分析如下:
客戶的機(jī)器的系統(tǒng)時(shí)間經(jīng)常出錯(cuò),導(dǎo)致給他們做的軟件無(wú)法正常使用,所以后來(lái)就加了一個(gè)同步網(wǎng)絡(luò)時(shí)間的小功能。實(shí)現(xiàn)起來(lái)很簡(jiǎn)單,但是卻很使用。
這個(gè)小功能就是先獲取網(wǎng)絡(luò)時(shí)間,然后將系統(tǒng)的時(shí)間修改成從網(wǎng)絡(luò)獲得的時(shí)間。下面是具體的實(shí)現(xiàn):
獲取網(wǎng)絡(luò)時(shí)間:
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)絡(luò)時(shí)間 /// </summary> public class NetTime { /// <summary> /// 獲取標(biāo)準(zhǔn)北京時(shí)間,讀取http://www.beijing-time.org/time.asp /// </summary> /// <returns>返回網(wǎng)絡(luò)時(shí)間</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)絡(luò)時(shí)間,返回一個(gè)DateTime對(duì)象,然后傳給設(shè)置系統(tǒng)時(shí)間的方法,修改系統(tǒng)時(shí)間。
同步系統(tǒng)時(shí)間:
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)時(shí)間 /// </summary> public class UpdateTime { //設(shè)置系統(tǒng)時(shí)間的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> /// 設(shè)置系統(tǒng)時(shí)間 /// </summary> /// <param name="dt">需要設(shè)置的時(shí)間</param> /// <returns>返回系統(tǒng)時(shí)間設(shè)置狀態(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; } }
兩個(gè)方法分別寫(xiě)在了兩個(gè)類里面,只需要在客戶端實(shí)例化兩個(gè)對(duì)象,然后依次調(diào)用其方法即可,簡(jiǎn)單實(shí)用。
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
- C# 委托的三種調(diào)用示例(同步調(diào)用 異步調(diào)用 異步回調(diào))
- c#(Socket)同步套接字代碼示例
- C#應(yīng)用BindingSource實(shí)現(xiàn)數(shù)據(jù)同步的方法
- c#.net多線程編程教學(xué)——線程同步
- C#同步、異步遠(yuǎn)程下載文件實(shí)例
- c#實(shí)現(xiàn)數(shù)據(jù)同步的方法(使用文件監(jiān)控對(duì)象filesystemwatcher)
- c#線程同步使用詳解示例
- 解析C#中委托的同步調(diào)用與異步調(diào)用(實(shí)例詳解)
- 基于C#實(shí)現(xiàn)的多生產(chǎn)者多消費(fèi)者同步問(wèn)題實(shí)例
- C#使用AutoResetEvent實(shí)現(xiàn)同步
相關(guān)文章
解析c#操作excel后關(guān)閉excel.exe的方法
C#和Asp.net下excel進(jìn)程一被打開(kāi),有時(shí)就無(wú)法關(guān)閉,尤其是website.對(duì)關(guān)閉該進(jìn)程有過(guò)GC、release等方法,但這些方法并不是在所有情況下均適用2013-07-07WPF實(shí)現(xiàn)3D翻牌式倒計(jì)時(shí)特效
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)3D翻牌式倒計(jì)時(shí)特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09詳解如何利用C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音的功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12.NET中實(shí)現(xiàn)彩色光標(biāo)、動(dòng)畫(huà)光標(biāo)及自定義光標(biāo)的方法
這篇文章主要介紹了.NET中實(shí)現(xiàn)彩色光標(biāo)、動(dòng)畫(huà)光標(biāo)及自定義光標(biāo)的方法,非常實(shí)用的功能,需要的朋友可以參考下2014-08-08C#實(shí)現(xiàn)航班預(yù)訂系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)航班預(yù)訂系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05