月夜钓钱江鱼

醉后不知天在水,满船清梦压星河。
posts - 50,comments - 8,views - 29026

先调用网络获取网络时间

namespace Utility
{
    /// <summary>
    /// 网络时间
    /// </summary>
    public class NetTime
    {
        /// <summary>
        /// 获取标准北京时间,读取http://www.beijing-time.org/time.asp
        /// </summary>
        /// <returns>返回网络时间</returns>
        public DateTime GetBeijingTime()
        {
            DateTime dt;
            WebRequest wrt = null;
            WebResponse wrp = null;
            try
            {

                wrt = WebRequest.Create("http://api.k780.com:88/?app=life.time&appkey=22839&sign=2e68c9cb4b8f921ed4031057e0eff33d&format=xml");
                wrp = wrt.GetResponse();
                string html = string.Empty;
                using (Stream stream = wrp.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
                    {
                        html = sr.ReadToEnd();
                    }
                }
                int head = html.IndexOf("datetime_1") + 11;
                int end = html.LastIndexOf("datetime_1") - 2;
                html = html.Substring(head, end - head);

                dt = DateTime.Parse(html);
            }

            catch (WebException)
            {
                return DateTime.Now;//DateTime.Parse("2011-1-1");
            }
            catch (Exception)
            {
                return DateTime.Now;//DateTime.Parse("2011-1-1");
            }
            finally
            {
                if (wrp != null)
                    wrp.Close();
                if (wrt != null)
                    wrt.Abort();
            }
            return dt;
        }
    }
}

在调用系统API设置系统时间更新

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;

namespace Utility
{
/// <summary>
/// 更新系统时间
/// </summary>
public class UpdateTime
{
    //设置系统时间的API函数
    [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;
    }
    /// <summary>
    /// 设置系统时间
    /// </summary>
    /// <param name="dt">需要设置的时间</param>
    /// <returns>返回系统时间设置状态,true为成功,false为失败</returns>
    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;
    }
}

}

这样就可以针对时间问题进行网络校时。

posted on   湘灵  阅读(182)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示