2023-02-16 18:12:17 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2022-06-09 09:21:32 +08:00
|
|
|
|
|
2023-02-16 18:12:17 +08:00
|
|
|
|
namespace Falcon.SugarApi.Windows
|
2022-06-09 09:21:32 +08:00
|
|
|
|
{
|
2023-03-02 10:39:50 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 系统类
|
|
|
|
|
/// </summary>
|
2023-02-16 18:12:17 +08:00
|
|
|
|
public partial class System
|
2022-06-09 09:21:32 +08:00
|
|
|
|
{
|
|
|
|
|
[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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DllImport("kernel32.dll")]
|
|
|
|
|
private static extern bool SetLocalTime(ref Systemtime time);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 设置Windows系统时间。应用程序必须以管理员身份运行,否则无法设置
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="dt">需要设置的时间</param>
|
|
|
|
|
/// <returns>返回系统时间设置状态,true为成功,false为失败</returns>
|
|
|
|
|
public static bool SetLocalDateTime(DateTime dt) {
|
|
|
|
|
Systemtime st;
|
2023-02-16 18:12:17 +08:00
|
|
|
|
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;
|
2022-06-09 09:21:32 +08:00
|
|
|
|
bool rt = SetLocalTime(ref st);
|
|
|
|
|
return rt;
|
|
|
|
|
}
|
2023-02-16 18:12:17 +08:00
|
|
|
|
|
2022-06-09 09:21:32 +08:00
|
|
|
|
}
|
2023-02-16 18:12:17 +08:00
|
|
|
|
}
|