【C#公共帮助类】DateTimeHelper设置电脑本地时间,实际开发很须要

  本文档主要为了解决实际开发当中,服务器和客户端电脑时间不能相等的问题,纯干货,实际项目这种时间不一样步的状况不少不少,时间不相等,到时候把本地的数据提交给服务器,服务器看实际上传时间和我写入数据库时间相差好大,影响实际业务操做和判断业务准确性,因此须要设置设备或者电脑的时间来为上传提供准确的时间节点。html

  欢迎传播分享,必须保持原做者的信息,但禁止将该文档直接用于商业盈利。web

  本人自从几年前走上编程之路,一直致力于收集和总结出好用的框架和通用类库,无论是微软本身的仍是第三方的只要实际项目中好用且能够解决实际问题那都会收集好,编写好文章和别人一块儿分享,这样本身学到了,别人也能学到知识,当今社会很须要知识的搬运工。数据库

     Email:707055073@qq.com
编程

  本文章地址:http://www.cnblogs.com/wohexiaocai/p/5721906.htmlwindows

1.基本介绍

      实际开发中,咱们须要测试电脑时间不是准确时间状况下对业务的影响就须要用到这个类,帮咱们设置电脑正确的时间点。服务器

     通常咱们是设置用户的电脑时间,不是服务器的时间哦,服务器的时间能够用NTP来进行统一设置的,科普:http://baike.baidu.com/link?url=Bq6U-qi2UFJSqMXaBJXNLemE69CEfpfrJmAx4HflvHgRIjP1v6hI2YZPyfabcgnjGNXP-yykPprAPtgRU3czna框架

2.如何获取正确的时间

要想获取到正确的时间,通常将咱们服务器的时间获取到就能够,服务器的时间通常都是联网的,时间比较准确,实际项目中咱们就是经过调用服务器的接口获取到当前时间的,这个并不难
 
 1 #region public static string GetResponse(string url)
 2         /// <summary>
 3         /// 获取一个网页
 4         /// </summary>
 5         /// <param name="url">地址</param>
 6         /// <returns>字符串返回值</returns>
 7         public static string GetResponse(string url)
 8         {
 9             string result = null;
10             WebResponse webResponse = null;
11             StreamReader streamReader = null;
12             try
13             {
14                 HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
15                 httpWebRequest.Method = "GET";
16                 webResponse = httpWebRequest.GetResponse();
17                 streamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
18                 result = streamReader.ReadToEnd();
19             }
20             catch
21             {
22                 // handle error
23             }
24             finally
25             {
26                 if (streamReader != null)
27                 {
28                     streamReader.Close();
29                 }
30                 if (webResponse != null)
31                 {
32                     webResponse.Close();
33                 }
34             }
35             return result;
36         }
37         #endregion
获取时间

3.设置电脑时间

获得时间就须要进行设置了,通常Windows系统都是经过DOS命令来设置电脑的时间因此这里也用了C#的DOS来设置

3.1 设置日期

 有时候只须要设置电脑的年月日ide

public static void SetLocalDate(int year, int month, int day)

3.2 设置时间

 有时候只须要设置电脑的时分秒测试

public static void SetLocalTime(int hour, int min, int sec)

3.3 设置年月日时分秒

 有时候只须要设置电脑的时分秒url

public static void SetLocalDateTime(DateTime time)

4.实战效果

通过win七、XP、windows server等测试后能够正常设置电脑时间,请你们能够正常使用,不实战怎么敢拿出来直接用呢?

5.源码下载

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Diagnostics;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace ConsoleApplication1
 9 {
10     public class DateTimeHelper
11     {
12         /// <summary>
13         /// 设置本地电脑的年月日
14         /// </summary>
15         /// <param name="year"></param>
16         /// <param name="month"></param>
17         /// <param name="day"></param>
18         public static void SetLocalDate(int year, int month, int day)
19         {
20             //实例一个Process类,启动一个独立进程 
21             Process p = new Process();
22             //Process类有一个StartInfo属性 
23             //设定程序名 
24             p.StartInfo.FileName = "cmd.exe";
25             //设定程式执行参数 “/C”表示执行完命令后立刻退出
26             p.StartInfo.Arguments = string.Format("/c date {0}-{1}-{2}", year, month, day);
27             //关闭Shell的使用 
28             p.StartInfo.UseShellExecute = false;
29             //重定向标准输入 
30             p.StartInfo.RedirectStandardInput = true;
31             p.StartInfo.RedirectStandardOutput = true;
32             //重定向错误输出 
33             p.StartInfo.RedirectStandardError = true;
34             //设置不显示doc窗口 
35             p.StartInfo.CreateNoWindow = true;
36             //启动 
37             p.Start();
38             //从输出流取得命令执行结果 
39             p.StandardOutput.ReadToEnd();
40         }
41 
42         /// <summary>
43         /// 设置本机电脑的时分秒
44         /// </summary>
45         /// <param name="hour"></param>
46         /// <param name="min"></param>
47         /// <param name="sec"></param>
48         public static void SetLocalTime(int hour, int min, int sec)
49         {
50             //实例一个Process类,启动一个独立进程 
51             Process p = new Process();
52             //Process类有一个StartInfo属性 
53             //设定程序名 
54             p.StartInfo.FileName = "cmd.exe";
55             //设定程式执行参数 “/C”表示执行完命令后立刻退出
56             p.StartInfo.Arguments = string.Format("/c time {0}:{1}:{2}", hour, min, sec);
57             //关闭Shell的使用 
58             p.StartInfo.UseShellExecute = false;
59             //重定向标准输入 
60             p.StartInfo.RedirectStandardInput = true;
61             p.StartInfo.RedirectStandardOutput = true;
62             //重定向错误输出 
63             p.StartInfo.RedirectStandardError = true;
64             //设置不显示doc窗口 
65             p.StartInfo.CreateNoWindow = true;
66             //启动 
67             p.Start();
68             //从输出流取得命令执行结果 
69             p.StandardOutput.ReadToEnd();
70         }
71 
72         /// <summary>
73         /// 设置本机电脑的年月日和时分秒
74         /// </summary>
75         /// <param name="time"></param>
76         public static void SetLocalDateTime(DateTime time)
77         {
78             SetLocalDate(time.Year, time.Month, time.Day);
79             SetLocalTime(time.Hour, time.Minute, time.Second);
80         }
81     }
82 }
相关文章
相关标签/搜索