zoukankan      html  css  js  c++  java
  • C# 时间校验器

    时间校验器处理过程:

    1. 从网络获得标准北京时间
    2. 解析时间
    3. 修改本地时间

    *注:对于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();
            }
        }
    }
    

      

  • 相关阅读:
    phpstorm使用svn爆出“cannot load supported formats” 的解决
    本地wamp的Internal Server Error错误解决方法
    mac下apache的多站点配置
    Git 一些错误的解决方法
    【总结整理】登录模块---摘自《人人都是产品经理》
    【总结整理】产品经理优秀品质----《结网》
    【总结整理】传统行业如何合理利用互联网思维----摘自《人人都是产品经理》
    【总结整理】租房产品创业的三个方向和三个产品---摘自《人人都是产品经理》
    【总结整理】KANO 模型
    【总结整理】关于GrowingIO、友盟、google analysis等数据分析
  • 原文地址:https://www.cnblogs.com/gracexu/p/2800915.html
Copyright © 2011-2022 走看看