using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Text.RegularExpressions; using System.IO; using System.Runtime.InteropServices; namespace pandian.cs { public class UpdateDateTime { public static DateTime dt = DateTime.Now; public static void UpdateTime() { try { Uri uri = new Uri("http://www.beijing-time.org/time15.asp"); System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser(); browser.DocumentCompleted += browser_DocumentCompleted; browser.Navigate(uri); } catch { } } static void browser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e) { try { string html = (sender as System.Windows.Forms.WebBrowser).Document.Body.OuterHtml; if (html.Last() == ‘;‘) { html = html.Remove(html.Length - 1); } string[] arr = html.Split(‘;‘); SystemTime st = new SystemTime(); foreach (string str in arr) { switch (str.Split(‘=‘)[0].Trim().ToLower()) { case "nyear": st.year = Convert.ToUInt16(str.Split(‘=‘)[1]); break; case "nmonth": st.month = Convert.ToUInt16(str.Split(‘=‘)[1]); break; case "nday": st.day = Convert.ToUInt16(str.Split(‘=‘)[1]); break; case "nhrs": st.hour = Convert.ToUInt16(str.Split(‘=‘)[1]); break; case "nmin": st.minute = Convert.ToUInt16(str.Split(‘=‘)[1]); break; case "nsec": st.second = Convert.ToUInt16(str.Split(‘=‘)[1]); break; } } SetLocalTime(st); } catch { } } /// <summary> ///系统时间类 /// </summary> [StructLayout(LayoutKind.Sequential)] public class SystemTime { public ushort year; public ushort month; public ushort dayofweek; public ushort day; public ushort hour; public ushort minute; public ushort second; public ushort milliseconds; } [DllImport("Kernel32.dll")] private static extern Boolean SetLocalTime([In, Out] SystemTime st); } }
时间: 2024-10-18 16:59:12