⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 手机时钟同步软件,使用GPRS连网,HTTP方式自动为服务器同步时间
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Net;

namespace AutoTimeSync
{
    /// <summary>
    /// 自动时间同步 V0.1.....
    /// 作    者: 边城浪
    /// 最后更改: 13:07 2007-9-17
    /// </summary>
    class Program
    {
        const string TimrUrl = "http://time.windows.com/";
        static void Main(string[] args)
        {
            int count = 0;
            while (count++ < 3)
            {
                try
                {
                    WebRequest request = WebRequest.Create(TimrUrl);
                    request.Timeout = 16000;
                    WebResponse response = request.GetResponse();
                    string time = response.Headers["Date"];
                    response.Close();
                    DateTime now = DateTime.Parse(time).AddSeconds(2);

                    TimeDate newtime = new TimeDate();
                    newtime.Year = (ushort)now.Year;
                    newtime.Month = (ushort)now.Month;
                    newtime.Week = (ushort)now.DayOfWeek;
                    newtime.Day = (ushort)now.Day;
                    newtime.Hour = (ushort)now.Hour;
                    newtime.Minute = (ushort)now.Minute;
                    newtime.Second = (ushort)now.Second;
                    newtime.Milliseconds = (ushort)now.Second;
                    if (SetLocalTime(ref newtime) > 0)
                        return;
                }
                catch 
                {
                    System.Threading.Thread.Sleep(4000);
                }
            }
        }

        [DllImport("coredll.dll")]
        public static extern int SetLocalTime(ref TimeDate time);      

        [StructLayout(LayoutKind.Sequential)]
        public struct TimeDate
        {
            public ushort Year;
            public ushort Month;
            public ushort Week;
            public ushort Day;
            public ushort Hour;
            public ushort Minute;
            public ushort Second;
            public ushort Milliseconds;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -