C# 时间校验器
时间校验器处理过程:
- 从网络获得标准北京时间
- 解析时间
- 修改本地时间
*注:对于Windows vista以上系统修改本地时间需要管理员权限,所以要用管理员权限打开开发工具,后续运行效验器时也要用管理员权限运行.
实现:
1 获得标准北京时间
WebRequest wrt = null;
WebResponse wrp = null;
wrt = WebRequest.Create("http://www.time.ac.cn/timeflash.asp?user=flash");
wrt.Credentials = CredentialCache.DefaultCredentials;
wrp = wrt.GetResponse();
StreamReader sr = new StreamReader(wrp.GetResponseStream(), Encoding.UTF8);
2 解析时间
string html = sr.ReadToEnd();
XElement root = XElement.Parse(html);
XElement element = root.Element("time");
int year = int.Parse(element.Element("year").Value);
int month = int.Parse(element.Element("month").Value);
int day = int.Parse(element.Element("day").Value);
int hour = int.Parse(element.Element("hour").Value);
int minite = int.Parse(element.Element("minite").Value);
int second = int.Parse(element.Element("second").Value);
3 修改本机系统时间
修改本机系统时间使用SetLocalTime Windows api实现.
首先设置结构体作为SetLocalTime的参数:
[StructLayout(LayoutKind.Sequential)]
public struct SystemTime {
public ushort wYear;
public ushort wMonth;
public ushort wDayOfWeek;
public ushort wDay;
public ushort wHour;
public ushort wMinute;
public ushort wSecond;
public ushort wMiliseconds;
}
引入SetLocalTime 函数:
[DllImport("Kernel32.dll")]
public static extern bool SetLocalTime(ref SystemTime sysTime);
修改时间:
SystemTime sysTime = new SystemTime();
sysTime.wYear = (ushort)year;
sysTime.wMonth = (ushort)month;
sysTime.wDay = (ushort)day;
sysTime.wHour = (ushort)hour;
sysTime.wMinute = (ushort)minite;
sysTime.wSecond = (ushort)second;
sysTime.wMiliseconds = 0;
if (!SetLocalTime(ref sysTime))
{
Console.WriteLine("设置失败!");
}
完整代码:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Net; using System.IO; using System.Xml; using System.Xml.Linq; using System.Runtime.InteropServices; namespace Test { [StructLayout(LayoutKind.Sequential)] public struct SystemTime { public ushort wYear; public ushort wMonth; public ushort wDayOfWeek; public ushort wDay; public ushort wHour; public ushort wMinute; public ushort wSecond; public ushort wMiliseconds; } class Program { [DllImport("Kernel32.dll")] public static extern bool SetSystemTime(ref SystemTime sysTime); [DllImport("Kernel32.dll")] public static extern bool SetLocalTime(ref SystemTime sysTime); static void Main(string[] args) { WebRequest wrt = null; WebResponse wrp = null; wrt = WebRequest.Create("http://www.time.ac.cn/timeflash.asp?user=flash"); wrt.Credentials = CredentialCache.DefaultCredentials; wrp = wrt.GetResponse(); StreamReader sr = new StreamReader(wrp.GetResponseStream(), Encoding.UTF8); string html = sr.ReadToEnd(); XElement root = XElement.Parse(html); XElement element = root.Element("time"); int year = int.Parse(element.Element("year").Value); int month = int.Parse(element.Element("month").Value); int day = int.Parse(element.Element("day").Value); int hour = int.Parse(element.Element("hour").Value); int minite = int.Parse(element.Element("minite").Value); int second = int.Parse(element.Element("second").Value); SystemTime sysTime = new SystemTime(); sysTime.wYear = (ushort)year; sysTime.wMonth = (ushort)month; sysTime.wDay = (ushort)day; sysTime.wHour = (ushort)hour; sysTime.wMinute = (ushort)minite; sysTime.wSecond = (ushort)second; sysTime.wMiliseconds = 0; if (!SetLocalTime(ref sysTime)) { Console.WriteLine("设置失败!"); } // Console.WriteLine(year); // Console.WriteLine(root.Element("month").Value); sr.Close(); wrp.Close(); Console.WriteLine("现在时间:{0}年{1}月{2}日 {3}:{4}:{5}",year,month,day,hour,minite,second); Console.ReadKey(); } } }