温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#同步网络时间和本地时间的代码

发布时间:2020-07-30 21:02:27 来源:网络 阅读:762 作者:zkspt 栏目:开发技术

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

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;
    }
}

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI