C#同步网络时间的方法实例详解

 更新时间:2015年05月04日 10:24:24   作者:刘水镜  
这篇文章主要介绍了C#同步网络时间的方法,以实例形式较为详细的分析了C#获取网络时间与同步本机系统时间的相关技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了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; 
 /// <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://www.beijing-time.org/time.asp");
    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("\r\n", "");
    }
    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; 
 /// <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;
  }
}

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

希望本文所述对大家的C#程序设计有所帮助。

相关文章

  • 解析c#操作excel后关闭excel.exe的方法

    解析c#操作excel后关闭excel.exe的方法

    C#和Asp.net下excel进程一被打开,有时就无法关闭,尤其是website.对关闭该进程有过GC、release等方法,但这些方法并不是在所有情况下均适用
    2013-07-07
  • 雅虎公司C#笔试题(后半部份才是)

    雅虎公司C#笔试题(后半部份才是)

    雅虎公司C#笔试题(后半部份才是)...
    2007-04-04
  • WPF实现3D翻牌式倒计时特效

    WPF实现3D翻牌式倒计时特效

    这篇文章主要为大家详细介绍了WPF实现3D翻牌式倒计时特效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09
  • C#延迟执行方法函数实例讲解

    C#延迟执行方法函数实例讲解

    这篇文章主要介绍了C#延迟执行方法函数实例讲解,这是比较常用的函数,有需要的同学可以研究下
    2021-03-03
  • 详解如何利用C#实现汉字转拼音功能

    详解如何利用C#实现汉字转拼音功能

    这篇文章主要为大家详细介绍了如何利用C#实现汉字转拼音的功能,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
    2022-12-12
  • .NET中实现彩色光标、动画光标及自定义光标的方法

    .NET中实现彩色光标、动画光标及自定义光标的方法

    这篇文章主要介绍了.NET中实现彩色光标、动画光标及自定义光标的方法,非常实用的功能,需要的朋友可以参考下
    2014-08-08
  • 详解c# 类的构造方法

    详解c# 类的构造方法

    本文主要介绍了c#类的构造方法。具有一定的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • C# WINFORM自定义异常处理方法

    C# WINFORM自定义异常处理方法

    这篇文章主要介绍了一个简单的统一异常处理方法。系统底层出现异常,写入记录文件,系统顶层捕获底层异常,显示提示信息。需要的可以参考一下
    2021-12-12
  • C#实现航班预订系统

    C#实现航班预订系统

    这篇文章主要为大家详细介绍了C#实现航班预订系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • C#中事件的定义和使用

    C#中事件的定义和使用

    在使用事件时,通常要定义两个方法,一个是和事件定义的委托签名一致的方法。下面让我们看看使用事件的具体步骤。
    2016-06-06

最新评论