设为首页 加入收藏

TOP

C# 同步更新系统时间(二)
2019-08-27 07:10:44 】 浏览:47
Tags:同步 更新 系统 时间
edUtcTime = Convert.ToDateTime(dateTimeva lue).Subtract(-k); 44 45 //处置北京时间 +8时 46 var updatedTime = updatedUtcTime.AddHours(8); 47 48 //转换System.DateTime到SystemTime 49 SystemTime systemTime = new SystemTime(); 50 systemTime.FromDateTime(updatedTime); 51 52 //调用Win32 API设置系统时间 53 Win32API.SetLocalTime(ref systemTime); 54 }

系统时间辅助类 & Win32API :

 1     /// <summary>
 2     /// 系统时间帮助类
 3     /// </summary>
 4     public struct SystemTime
 5     {
 6         public ushort wYear;
 7         public ushort wMonth;
 8         public ushort wDayOfWeek;
 9         public ushort wDay;
10         public ushort wHour;
11         public ushort wMinute;
12         public ushort wSecond;
13         public ushort wMilliseconds;
14 
15         /// <summary>
16         /// 从System.DateTime转换。
17         /// </summary>
18         /// <param name="time">System.DateTime类型的时间。</param>
19         public void FromDateTime(DateTime time)
20         {
21             wYear = (ushort)time.Year;
22             wMonth = (ushort)time.Month;
23             wDayOfWeek = (ushort)time.DayOfWeek;
24             wDay = (ushort)time.Day;
25             wHour = (ushort)time.Hour;
26             wMinute = (ushort)time.Minute;
27             wSecond = (ushort)time.Second;
28             wMilliseconds = (ushort)time.Millisecond;
29         }
30         /// <summary>
31         /// 转换为System.DateTime类型。
32         /// </summary>
33         /// <returns></returns>
34         public DateTime ToDateTime()
35         {
36             return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
37         }
38         /// <summary>
39         /// 静态方法。转换为System.DateTime类型。
40         /// </summary>
41         /// <param name="time">SYSTEMTIME类型的时间。</param>
42         /// <returns></returns>
43         public static DateTime ToDateTime(SystemTime time)
44         {
45             return time.ToDateTime();
46         }
47     }
48 
49     /// <summary>
50     /// 系统更新时间DLL
51     /// </summary>
52     public class Win32API
53     {
54         [DllImport("Kernel32.dll")]
55         public static extern bool SetLocalTime(ref SystemTime Time);
56         [DllImport("Kernel32.dll")]
57         public static extern void GetLocalTime(ref SystemTime Time);
58     }
View Code

 

Github地址:IE环境修复工具

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇当前项目规划 下一篇第四章vs2107 代码实际运用-后台..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目