• 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();
            }
        }
    }
    

      

  • 相关阅读:
    JS小白进阶之路(2)
    JS小白进阶之路(1)
    ajax.readyState与ajax.status一览
    Photoshop投影和CSS box-shadow转换
    layer弹层插件
    [Intervention] Ignored attempt to cancel a touchmove event with cancelable=false, for example because scrolling is in progress and cannot be interrupted
    css清除浮动影响
    css网页重置样式表(多版本)
    XSS攻击和CSRF攻击的定义及区别
    git的cd命令
  • 原文地址:https://www.cnblogs.com/gracexu/p/2800915.html
Copyright © 2020-2023  润新知