C# 如何使用 .NET 更新系统的日期和/或时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/521868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to update the system's date and/or time using .NET
提问by user62958
I am trying to update my system time using the following:
我正在尝试使用以下方法更新我的系统时间:
[StructLayout(LayoutKind.Sequential)]
private 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;
}
[DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
private extern static void Win32GetSystemTime(ref SYSTEMTIME lpSystemTime);
[DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
private extern static bool Win32SetSystemTime(ref SYSTEMTIME lpSystemTime);
public void SetTime()
{
TimeSystem correctTime = new TimeSystem();
DateTime sysTime = correctTime.GetSystemTime();
// Call the native GetSystemTime method
// with the defined structure.
SYSTEMTIME systime = new SYSTEMTIME();
Win32GetSystemTime(ref systime);
// Set the system clock ahead one hour.
systime.wYear = (ushort)sysTime.Year;
systime.wMonth = (ushort)sysTime.Month;
systime.wDayOfWeek = (ushort)sysTime.DayOfWeek;
systime.wDay = (ushort)sysTime.Day;
systime.wHour = (ushort)sysTime.Hour;
systime.wMinute = (ushort)sysTime.Minute;
systime.wSecond = (ushort)sysTime.Second;
systime.wMilliseconds = (ushort)sysTime.Millisecond;
Win32SetSystemTime(ref systime);
}
When I debug everything looks good and all the values are correct but when it calles the Win32SetSystemTime(ref systime) th actual time of system(display time) doesn't change and stays the same. The strange part is that when I call the Win32GetSystemTime(ref systime) it gives me the new updated time. Can someone give me some help on this?
当我调试时,一切看起来都很好,所有的值都是正确的,但是当它调用 Win32SetSystemTime(ref systime) 时,系统的实际时间(显示时间)不会改变并保持不变。奇怪的是,当我调用 Win32GetSystemTime(ref systime) 时,它给了我新的更新时间。有人可以给我一些帮助吗?
回答by David Morton
According to the code you have right there, you're not incrementing the hour. It looks like you're setting your system time to the exact same time as it was when you called Win32GetSystemTime.
根据你在那里的代码,你没有增加小时。看起来您将系统时间设置为与调用 Win32GetSystemTime 时完全相同的时间。
Try:
尝试:
systime.wHour = (ushort)(sysTime.Hour + 1);
回答by JaredPar
Part of your problem is that you have a couple of incorrect PInvoke signatures. Most notable SetSystemTime should have a non-void return value. Here is the correct signature
您的部分问题是您有几个不正确的 PInvoke 签名。最值得注意的 SetSystemTime 应该有一个非空的返回值。这是正确的签名
/// Return Type: BOOL->int
///lpSystemTime: SYSTEMTIME*
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint="SetSystemTime")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool SetSystemTime([InAttribute()] ref SYSTEMTIME lpSystemTime) ;
My suspicion is that the lock of a return value messed up the stack and the SetSystemTime function essentially ended up with bad data.
我怀疑是返回值的锁定弄乱了堆栈,并且 SetSystemTime 函数基本上以错误数据结束。
回答by Lei
The problem is about UTC time and local time. See this link: http://www.codeproject.com/Messages/2998246/problem-with-SetSystemTime-fucntion-of-Kernel32-dl.aspx
问题是关于 UTC 时间和本地时间。请参阅此链接:http: //www.codeproject.com/Messages/2998246/problem-with-SetSystemTime-fucntion-of-Kernel32-dl.aspx
Hope this maybe helpful for you.
希望这可能对您有所帮助。
回答by mtaskopru
TimeSpan diffLocalTime = DateTime.Now - DateTime.UtcNow;
SystemTime systemTime = new SystemTime();
systemTime.Second = ...
systemTime.Minute =....
systemTime.Hour -= (ushort)(diffLocalTime.Hours + 1); // 00:59:59 I rounded as 1 hour
bool result = SetSystemTime(ref systemTime);
This using It's working correctly for local time zone.
这使用它在本地时区正常工作。
回答by erfan
[DllImport("Kernel32.dll")]
public static extern bool SetLocalTime(ref StractData.Datetime.SYSTEMTIME Time);
回答by user2118273
You have just a privileges problem, when I run that program without privileges, it does'nt changes the date, but if i do right click on the program, and click on run as administrator, it works!
您只是权限问题,当我在没有权限的情况下运行该程序时,它不会更改日期,但是如果我右键单击该程序,然后单击以管理员身份运行,它就可以工作!
回答by Jaans
No need for P/Invoke - there is a simpler way (but not well known) available from the Microsoft.VisualBasic assembly. If you are in C#, just remember to add a reference to it.
不需要 P/Invoke - Microsoft.VisualBasic 程序集中有一种更简单的方法(但并不为人所知)。如果您使用的是 C#,请记住添加对它的引用。
You can use the Microsoft.VisualBasic.DateAndTime
class to get and change the Date or Time. The properties Today
and TimeOfDay
have setters that will make the changes to the date or time.
您可以使用Microsoft.VisualBasic.DateAndTime
该类来获取和更改日期或时间。属性Today
和TimeOfDay
设置器将更改日期或时间。
You still need the relevant privileges - refer to the MSDN documentation link below.
您仍然需要相关权限 - 请参阅下面的 MSDN 文档链接。
Here's an arbitrary example to change the time:
这是更改时间的任意示例:
public static void SetTimeToPast()
{
Microsoft.VisualBasic.DateAndTime.TimeOfDay = DateTime.Now.AddMinutes( -2 );
}
Reference on MSDN: http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.dateandtime(v=vs.110).aspx
MSDN 上的参考:http: //msdn.microsoft.com/en-us/library/microsoft.visualbasic.dateandtime(v= vs.110).aspx