男女做爰视频免费网站,备案 网站名称什么用,app推广平台有哪些,做网站预算在日常工作中#xff0c;有时可能会需要获取或修改客户端电脑的系统时间#xff0c;比如软件设置了Licence有效期#xff0c;预计2024-06-01 00:00:00到期#xff0c;如果客户手动修改了客户端电脑时间#xff0c;往前调整了一年#xff0c;则软件就可以继续使用一年有时可能会需要获取或修改客户端电脑的系统时间比如软件设置了Licence有效期预计2024-06-01 00:00:00到期如果客户手动修改了客户端电脑时间往前调整了一年则软件就可以继续使用一年如此循环往复则Licence将形同虚设。所以有时候需要校验客户端电脑时间和服务器端时间是否一致如果不一致则需要修改客户端电脑时间或进行系统提示。本文以一个简单的小例子简述如何通过C#获取和设置客户端电脑的系统时间仅供学习分享使用如有不足之处还请指正。 涉及知识点 在windows系统中设置系统时间主要通过win32提供的API来实现如下所示
SetLocalTime 设置系统的本地化时间GetLocalTime 获取系统的本地化时间SetSystemTime 设置系统的Utc时间GetSystemTime 获取系统的Utc时间 核心代码 时间结构体 在上述四个系统函数中都需要一个时间类型的结构体包含时分秒年月日。如下所示 [StructLayout(LayoutKind.Sequential)]
public struct SystemTime
{public ushort wYear;public ushort wMonth;public ushort wDayOfWeek;public ushort wDay;public ushort wHour;public ushort wMinute;public ushort wSecond;public ushort wMilliseconds;public override string ToString(){return ${wYear}-{wMonth}-{wDay} {wHour}:{wMinute}:{wSecond}.{wMilliseconds};}
} 系统时间帮助类 为了方便调用将4个系统函数进行封装到一个类中SysTimeHelper如下所示 public class SysTimeHelper
{[DllImport(kernel32.dll)]public static extern bool SetSystemTime(ref SystemTime st);[DllImport(Kernel32.dll)]public static extern bool SetLocalTime(ref SystemTime st);[DllImport(Kernel32.dll)]public static extern void GetSystemTime(ref SystemTime st);[DllImport(Kernel32.dll)]public static extern void GetLocalTime(ref SystemTime st);public static string GetLocalTime(){SystemTime st new SystemTime();GetLocalTime(ref st);return st.ToString();}public static bool SetLocalTimeByStr(string timestr){bool flag false;SystemTime sysTime new SystemTime();DateTime dt Convert.ToDateTime(timestr);sysTime.wYear Convert.ToUInt16(dt.Year);sysTime.wMonth Convert.ToUInt16(dt.Month);sysTime.wDay Convert.ToUInt16(dt.Day);sysTime.wHour Convert.ToUInt16(dt.Hour);sysTime.wMinute Convert.ToUInt16(dt.Minute);sysTime.wSecond Convert.ToUInt16(dt.Second);try{flag SetLocalTime(ref sysTime);}catch (Exception ex){string e ex.Message;return false;}return flag;}/// summary /// 时间戳转为C#格式时间 /// /summary /// param name”timeStamp”/param /// returns/returns public static DateTime ConvertStringToDateTime(string timeStamp){DateTime dtStart TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));long lTime long.Parse(timeStamp 0000);TimeSpan toNow new TimeSpan(lTime);return dtStart.Add(toNow);}/// summary/// 时间戳转为C#格式时间10位/// /summary/// param nametimeStampUnix时间戳格式/param/// returnsC#格式时间/returnspublic static DateTime GetDateTimeFrom1970Ticks(long curSeconds){DateTime dtStart TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));return dtStart.AddSeconds(curSeconds);}} 函数调用 在页面调用时即可通过SysTimeHelper帮助类进行获取和修改系统时间。如下所示 public partial class MainWindow : Window
{public MainWindow(){InitializeComponent();}private void Window_Loaded(object sender, RoutedEventArgs e){var time SysTimeHelper.GetLocalTime();this.txtTime.Text time;}private void Button_Click(object sender, RoutedEventArgs e){var time this.txtTime.Text;bool flag SysTimeHelper.SetLocalTimeByStr(time);if(flag){MessageBox.Show(设置成功);}else{MessageBox.Show(设置失败);}}
} 实例演示 通过VS运行程序在打开程序时获取时间然后手动修改时间点击设置如下所示 如果设置过后想要回复可通过设置页面【同步时钟】进行恢复如下所示 注意如果在调试时设置失败【SetLocalTime返回false】可通过【以管理员身份运行】的方式打开Visual Studio如下所示 或者直接通过【以管理员身份运行】启动程序如下所示 以上就是【C# 通过Win32API设置客户端系统时间】的全部内容希望可以抛砖引玉一起学习共同进步。