1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 using System.Net; 7 using System.Net.Sockets; 8 using System.Text.RegularExpressions; 9 using System.Runtime.InteropServices; 10 using System.Runtime; 11 12 13 14 /// <summary> 15 /// 网络时间 16 /// </summary> 17 public class NetTime 18 { 19 20 /// <summary> 21 /// 获取标准北京时间,读取http://www.beijing-time.org/time.asp 22 /// </summary> 23 /// <returns>返回网络时间</returns> 24 public DateTime GetBeijingTime() 25 { 26 27 DateTime dt; 28 WebRequest wrt = null; 29 WebResponse wrp = null; 30 try 31 { 32 wrt = WebRequest.Create("http://www.beijing-time.org/time.asp"); 33 wrp = wrt.GetResponse(); 34 35 string html = string.Empty; 36 using (Stream stream = wrp.GetResponseStream()) 37 { 38 using (StreamReader sr = new StreamReader(stream, Encoding.UTF8)) 39 { 40 html = sr.ReadToEnd(); 41 } 42 } 43 44 string[] tempArray = html.Split(';'); 45 for (int i = 0; i < tempArray.Length; i++) 46 { 47 tempArray[i] = tempArray[i].Replace(" ", ""); 48 } 49 50 string year = tempArray[1].Split('=')[1]; 51 string month = tempArray[2].Split('=')[1]; 52 string day = tempArray[3].Split('=')[1]; 53 string hour = tempArray[5].Split('=')[1]; 54 string minite = tempArray[6].Split('=')[1]; 55 string second = tempArray[7].Split('=')[1]; 56 57 dt = DateTime.Parse(year + "-" + month + "-" + day + " " + hour + ":" + minite + ":" + second); 58 } 59 catch (WebException) 60 { 61 return DateTime.Parse("2011-1-1"); 62 } 63 catch (Exception) 64 { 65 return DateTime.Parse("2011-1-1"); 66 } 67 finally 68 { 69 if (wrp != null) 70 wrp.Close(); 71 if (wrt != null) 72 wrt.Abort(); 73 } 74 return dt; 75 76 } 77 }
获取网络时间,返回一个DateTime对象,然后传给设置系统时间的方法,修改系统时间。下面是设置系统时间的代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 using System.Net; 7 using System.Net.Sockets; 8 using System.Text.RegularExpressions; 9 using System.Runtime.InteropServices; 10 using System.Runtime; 11 12 13 /// <summary> 14 /// 更新系统时间 15 /// </summary> 16 public class UpdateTime 17 { 18 //设置系统时间的API函数 19 [DllImport("kernel32.dll")] 20 private static extern bool SetLocalTime(ref SYSTEMTIME time); 21 22 [StructLayout(LayoutKind.Sequential)] 23 private struct SYSTEMTIME 24 { 25 public short year; 26 public short month; 27 public short dayOfWeek; 28 public short day; 29 public short hour; 30 public short minute; 31 public short second; 32 public short milliseconds; 33 } 34 35 /// <summary> 36 /// 设置系统时间 37 /// </summary> 38 /// <param name="dt">需要设置的时间</param> 39 /// <returns>返回系统时间设置状态,true为成功,false为失败</returns> 40 public static bool SetDate(DateTime dt) 41 { 42 SYSTEMTIME st; 43 44 st.year = (short)dt.Year; 45 st.month = (short)dt.Month; 46 st.dayOfWeek = (short)dt.DayOfWeek; 47 st.day = (short)dt.Day; 48 st.hour = (short)dt.Hour; 49 st.minute = (short)dt.Minute; 50 st.second = (short)dt.Second; 51 st.milliseconds = (short)dt.Millisecond; 52 bool rt = SetLocalTime(ref st); 53 return rt; 54 } 55 }
需要注意的时,在win8系统上需要以管理员身份来运行程序,否则是无法设置系统时间的。下面这段代码可以设置让程序默认以管理员身份运行:
1 static void Main(string[] Args) 2 { 3 /** 4 * 当前用户是管理员的时候,直接启动应用程序 5 * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 6 */ 7 //获得当前登录的Windows用户标示 8 System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent(); 9 //创建Windows用户主题 10 Application.EnableVisualStyles(); 11 12 System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity); 13 //判断当前登录用户是否为管理员 14 if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) 15 { 16 //如果是管理员,则直接运行 17 18 Application.EnableVisualStyles(); 19 Application.Run(new Form1()); 20 } 21 else 22 { 23 //创建启动对象 24 System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); 25 //设置运行文件 26 startInfo.FileName = System.Windows.Forms.Application.ExecutablePath; 27 //设置启动参数 28 startInfo.Arguments = String.Join(" ", Args); 29 //设置启动动作,确保以管理员身份运行 30 startInfo.Verb = "runas"; 31 //如果不是管理员,则启动UAC 32 System.Diagnostics.Process.Start(startInfo); 33 //退出 34 System.Windows.Forms.Application.Exit(); 35 } 36 }
搞定!