C#同步网络时间和本地时间的代码-创新互联

做工程过程,将做工程过程经常用的内容做个收藏,下面内容内容是关于 C#同步网络时间和本地时间的内容,希望对各位朋友有较大用途。

创新互联公司是一家专注于成都网站设计、成都做网站与策划设计,婺城网站建设哪家好?创新互联公司做网站,专注于网站建设十余年,网设计领域的专业建站公司;建站业务涵盖:婺城等地区。婺城做网站价格咨询:18982081108

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Runtime;

public class NetTime
{

    public DateTime GetBeijingTime()
    {

        DateTime dt;
       webRequest wrt = null;
       webResponse wrp = null;
        try
        {
           wrp = wrt.GetResponse();

            string html = string.Empty;
            using (Stream stream = wrp.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
                {
                    html = sr.ReadToEnd();
                }
            }

            string[] tempArray = html.Split(';');
            for (int i = 0; i < tempArray.Length; i++)
            {
                tempArray[i] = tempArray[i].Replace("rn", "");
            }

            string year = tempArray[1].Split('=')[1];
            string month = tempArray[2].Split('=')[1];
            string day = tempArray[3].Split('=')[1];
            string hour = tempArray[5].Split('=')[1];
            string minite = tempArray[6].Split('=')[1];
            string second = tempArray[7].Split('=')[1];

            dt = DateTime.Parse(year + "-" + month + "-" + day + " " + hour + ":" + minite + ":" + second);
        }
        catch (WebException)
        {
            return DateTime.Parse("2011-1-1");
        }
        catch (Exception)
        {
            return DateTime.Parse("2011-1-1");
        }
        finally
        {
            if (wrp != null)
               wrp.Close();
            if (wrt != null)
               wrt.Abort();
        }
        return dt;

    }
}

获取网络时间,返回一个DateTime对象,然后传给设置系统时间的方法,修改系统时间。同步系统时间:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Runtime;

public class UpdateTime
{
    [DllImport("kernel32.dll")]
    private static extern bool SetLocalTime(ref SYSTEMTIME time);

    [StructLayout(LayoutKind.Sequential)]
    private struct 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;
    }

    public static bool SetDate(DateTime dt)
    {
        SYSTEMTIME st;

        st.year = (short)dt.Year;
        st.month = (short)dt.Month;
        st.dayOfWeek = (short)dt.DayOfWeek;
        st.day = (short)dt.Day;
        st.hour = (short)dt.Hour;
        st.minute = (short)dt.Minute;
        st.second = (short)dt.Second;
        st.milliseconds = (short)dt.Millisecond;
        bool rt = SetLocalTime(ref st);
        return rt;
    }
}

两个方法分别写在了两个类里面,只需要在客户端实例化两个对象,然后依次调用其方法即可,简单实用。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站名称:C#同步网络时间和本地时间的代码-创新互联
路径分享:http://lszwz.com/article/dhjgds.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款