Falcon.SugarApi/Falcon.SugarApi.Windows/System.cs
2023-02-16 18:15:51 +08:00

43 lines
1.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Runtime.InteropServices;
namespace Falcon.SugarApi.Windows
{
public partial class System
{
[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;
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;
}
}
}