首页 > 代码库 > 修改操作系统时间
修改操作系统时间
internal class LocalTimeHelper { [StructLayout(LayoutKind.Sequential)] struct SystemTime { public UInt16 Year; public UInt16 Month; public UInt16 DayOfWeek; public UInt16 Day; public UInt16 Hour; public UInt16 Minute; public UInt16 Second; public UInt16 Millisecond; public DateTime ToDateTime() { return new DateTime(Year, Month, Day, Hour, Minute, Second, Millisecond); } public SystemTime FromDateTime(DateTime value) { Year = (UInt16)value.Year; Month = (UInt16)value.Month; Day = (UInt16)value.Day; Hour = (UInt16)value.Hour; Minute = (UInt16)value.Minute; Second = (UInt16)value.Second; Millisecond = (UInt16)value.Millisecond; return this; } public static implicit operator DateTime(SystemTime value) { return value.ToDateTime(); } public static implicit operator SystemTime(DateTime value) { return new SystemTime().FromDateTime(value); } } [DllImport("kernel32.dll")] static extern int SetSystemTime(ref SystemTime systemTime); [DllImport("Kernel32.dll")] static extern void GetSystemTime(ref SystemTime sysTime); public static bool SetSystemLocalTime(DateTime value) { SystemTime time = value.ToUniversalTime(); var res = SetSystemTime(ref time); return res != 0; } public static DateTime GetSystemLocalTime() { var time = new SystemTime(); GetSystemTime(ref time); DateTime res = ((DateTime)time).ToLocalTime(); return res; } }
修改操作系统时间
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。