最好是使用平台调用,下面是一段示例代码,调用其它API同样道理:
[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 DateTime ToDateTime()
{
return new DateTime(wYear, wMonth, wDay, wHour, wMinute, wSecond, wMilliseconds);
}
public SYSTEMTIME ToSystemTime(DateTime dt)
{
SYSTEMTIME st;
st.wYear = (ushort)dt.Year;
st.wMonth = (ushort)dt.Month;
st.wDayOfWeek = (ushort)dt.DayOfWeek;
st.wDay = (ushort)dt.Day;
st.wHour = (ushort)dt.Hour;
st.wMinute = (ushort)dt.Minute;
st.wSecond = (ushort)dt.Second;
st.wMilliseconds = (ushort)dt.Millisecond;
return st;
}
}
public class Win32API
{
[DllImport("kernel32.dll")]
public static extern bool SetSystemTime(ref SYSTEMTIME st);
}
要引用System.Runtime.InteropServices;命名空间