zoukankan      html  css  js  c++  java
  • An easy way to syncTime using C#

    /*
     * Created by SharpDevelop.
     * User: Administrator
     * Date: 2013/10/23
     * Time: 8:57
     * author zibet
     */
    using System;
    using System.Net;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    namespace SyncTime
    {
    	/// <summary>
    	/// Description of MainForm.
    	/// </summary>
    	public partial class MainForm : Form
    	{
    		[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;
            }
    		public MainForm() {
    			InitializeComponent();
    		}
            [DllImport("Kernel32.dll")]
            public static extern bool SetLocalTime(ref SystemTime sysTime);
            [DllImport("Kernel32.dll")]
            public static extern void GetLocalTime(ref SystemTime localTime);
    		private DateTime getIntetime(){
    			return DateTime.Parse(WebRequest.Create("http://www.baidu.com/").GetResponse().Headers.Get("Date"));
    		}
    		void MainFormLoad(object sender, EventArgs e)
    		{
    			SystemTime st = new SystemTime();
    			DateTime BJTime = getIntetime();
    			st.wYear = Convert.ToUInt16(BJTime.Year);
    			st.wMonth = Convert.ToUInt16(BJTime.Month);
    			st.wDay = Convert.ToUInt16(BJTime.Day);
    			st.wHour = Convert.ToUInt16(BJTime.Hour);
    			st.wMinute = Convert.ToUInt16(BJTime.Minute);
    			st.wSecond = Convert.ToUInt16(BJTime.Second);
    			if (!SetLocalTime(ref st)) {
    				MessageBox.Show("Sync failed T-T");
    				return;
    			}
    			MessageBox.Show("Sync done!");
    				
    		}
    	}
    }
    
  • 相关阅读:
    DataTable的一些使用技巧
    Linux下使用Mysql
    【Cocos2d-X开发学习笔记】第28期:游戏中音乐和音效的使用
    HDU 4669 Mutiples on a circle (DP , 统计)
    面试经典-设计包含min函数的栈
    最大熵模型
    这篇文章关于两阶段提交和Paxos讲的很好
    【读书笔记】神经网络与深度学习
    这个对协程的分析不错
    sendfile学习
  • 原文地址:https://www.cnblogs.com/cause/p/3398718.html
Copyright © 2011-2022 走看看