zoukankan      html  css  js  c++  java
  • c# 获取北京时间更新本地计算机

         class UpdateDateTime
         {
            [DllImport("Kernel32.dll")]
            private static extern void SetLocalTime([In, Out]   SystemTime st); 
    
            public static void UpdateTime()
            {
                Uri uri = new Uri("http://www.beijing-time.org/time15.asp");
                WebRequest request = WebRequest.Create(uri);
                WebResponse response = request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gbk"));
    
                string html = reader.ReadToEnd();
                reader.Close();
                reader.Dispose();
                response.Close();
    
                if (html[html.Length-1].Equals(';'))
                {
                    html = html.Remove(html.Length - 1);
                }
              
                string[] arr = html.Split(';');
                SystemTime st = new SystemTime();
    
                foreach (string str in arr)
                {
                    switch (str.Split('=')[0].Trim().ToLower())
                    {
                        case "nyear":
                            st.Year = Convert.ToInt16(str.Split('=')[1]);
                            break;
                        case "nmonth":
                            st.Month = Convert.ToInt16(str.Split('=')[1]);
                            break;
                        case "nday":
                            st.Day = Convert.ToInt16(str.Split('=')[1]);
                            break;
                        case "nhrs":
                            st.Hour = Convert.ToInt16(str.Split('=')[1]);
                            break;
                        case "nmin":
                            st.Minute = Convert.ToInt16(str.Split('=')[1]);
                            break;
                        case "nsec":
                            st.Second = Convert.ToInt16(str.Split('=')[1]);
                            break;
                    }
                }
                SetLocalTime(st);
            }
        }
    
    
        [StructLayout(LayoutKind.Sequential)]
        class SystemTime
        {
            /// 
            ///系统时间类 
            ///         
            public short Year;
            public short Month;
            public short DayOfWeek;
            public short Day;
            public short Hour;
            public short Minute;
            public short Second;
            public short Milliseconds;
        } 
  • 相关阅读:
    laravel-admin 关闭debug模式导致异常信息到页面的排查
    laravel-sql
    laravel任务调度出现僵尸进程
    PHP获取首字母笔记
    IP库笔记
    深入理解 js 闭包
    用键盘实现上下选择
    密码保护
    评分效果
    数组去重
  • 原文地址:https://www.cnblogs.com/caoyc/p/5087928.html
Copyright © 2011-2022 走看看