我有以下 C# 代码:
public static void SetDateTime(System.DateTime datetime)
{
var systemNew = new SystemTime
{
wDay = (ushort)datetime.Day,
wMonth = (ushort)datetime.Month,
wYear = (ushort)datetime.Year,
wHour = (ushort)datetime.Hour,
wMinute = (ushort)datetime.Minute,
wSecond = (ushort)datetime.Second,
wMilliseconds = (ushort)datetime.Millisecond
};
// update system clock
NativeMethods.SetLocalTime(ref systemNew);
int errorCode = Marshal.GetLastWin32Error();
if (errorCode != 0)
{
Debug.WriteLine("SetDateTime error: {0}", errorCode);
}
}
internal struct SystemTime
{
internal ushort wDay;
internal ushort wDayOfWeek;
internal ushort wHour;
internal ushort wMilliseconds;
internal ushort wMinute;
internal ushort wMonth;
internal ushort wSecond;
internal ushort wYear;
}
internal static class NativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern int SetLocalTime(ref SystemTime lpSystemTime);
}
但是,每次我调用该方法时,这都会给我错误代码 87,有什么想法吗?
编辑:我在我的本地机器上检查了它,它在那里工作正常。 它不适用于 Visual Studio 中的远程调试,即使远程进程在与我的本地帐户相同的帐户下运行并且它是该系统上的管理员。