C# 修改系統時間
阿新 • • 發佈:2019-04-20
修改 配置 fcm 設置 eas 圖片 code text urn
/// <summary> /// 同步服務時間 /// </summary> public class SyncServerTime { //設置系統時間的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; } }
運行中發現總是返回false。
經過研究發現原來時我的程序運行在win8系統上需要管理員權限,然後程序作如下配置即可:
C# 修改系統時間