家里的破电脑bios电池没电了,时间总是保存不了。于是写了个程序从百度服务器上获取时间,每次开机更新一下就OK了。
这里是控制台程序,放在启动文件夹下面开机就会自动运行了。当然如果你受不了每次开机都弹控制台的黑窗口也可以封成window服务。
更新系统时间使用VB.NET的API方便快捷! 时间从百度服务器上获取,当然这种方法获取的时间有误差,只精确到分,如果网速不给力的话也会影响误差。
对我来收这个误差可以接受了!
1
using System;
2 using System.Net;
3
4 namespace SetTime
5 {
6 class Program
7 {
8 static void Main( string[] args)
9 {
10 try
11 {
12 Console.WriteLine( " 更新时间开始! ");
13 DateTime dt = GetNowTimeFromBaidu();
14 Microsoft.VisualBasic.DateAndTime.Today = dt;
15 Microsoft.VisualBasic.DateAndTime.TimeOfDay = dt;
16 Console.WriteLine( " 更新完成! ");
17 }
18 catch (Exception e)
19 {
20 Console.WriteLine(e.Message);
21 Console.Read();
22 }
23
24 }
25
26 public static DateTime GetNowTimeFromBaidu()
27 {
28 HttpWebRequest request = (HttpWebRequest)WebRequest.Create( new Uri( " http://www.baidu.com/ "));
29 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
30 string date = response.Headers[ " Date "];
31 return DateTime.Parse(date);
32 }
33
34 }
35 }
2 using System.Net;
3
4 namespace SetTime
5 {
6 class Program
7 {
8 static void Main( string[] args)
9 {
10 try
11 {
12 Console.WriteLine( " 更新时间开始! ");
13 DateTime dt = GetNowTimeFromBaidu();
14 Microsoft.VisualBasic.DateAndTime.Today = dt;
15 Microsoft.VisualBasic.DateAndTime.TimeOfDay = dt;
16 Console.WriteLine( " 更新完成! ");
17 }
18 catch (Exception e)
19 {
20 Console.WriteLine(e.Message);
21 Console.Read();
22 }
23
24 }
25
26 public static DateTime GetNowTimeFromBaidu()
27 {
28 HttpWebRequest request = (HttpWebRequest)WebRequest.Create( new Uri( " http://www.baidu.com/ "));
29 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
30 string date = response.Headers[ " Date "];
31 return DateTime.Parse(date);
32 }
33
34 }
35 }