• 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;
        } 
  • 相关阅读:
    面试精选:链表问题集锦
    经典排序算法总结与实现 ---python
    Python高级编程–正则表达式(习题)
    Python面试题汇总
    Python正则表达式
    Linux下的Libsvm使用历程录
    在 linux(ubuntu) 下 安装 LibSVM
    过拟合
    百度历年笔试面试150题
    MATLAB 的数据类型
  • 原文地址:https://www.cnblogs.com/caoyc/p/5087928.html
Copyright © 2020-2023  润新知