2009-02-06 18 views
8

我试图用更新我的系统时间,下面来更新系统的日期和/或时间:如何使用.NET

[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); 
} 

当调试一切看起来不错,所有的值都是正确的,但是当它调用Win32SetSystemTime(ref systime)系统的实际时间(显示时间)不会更改并保持不变。奇怪的是,当我调用Win32GetSystemTime(ref systime)时,它给了我新的更新时间。有人能给我一些帮助吗?

+0

我看到,TimeSystem是从基本系统类非,你确定TimeSystem不改变时间?尝试评论Win32SetSystemTime(ref systime);行 – Avram 2009-02-08 00:30:41

回答

4

根据你在那里的代码,你不会增加小时。看起来您正在将系统时间设置为与您调用Win32GetSystemTime时相同的时间。

尝试:你的问题的

systime.wHour = (ushort)(sysTime.Hour + 1); 
+0

但我不想增加小时。我想把它设置到另一个时间。 – user62958 2009-02-06 19:51:06

+0

您的代码中的代码注释特别声明“将系统时钟提前一小时”。仅仅因为你没有改变它,你并没有改变你的系统时间。要将它设置为另一次,必须将发送到方法中的结构设置为与当前时间不同的时间。 – 2009-02-06 20:07:48

6

部分原因是,你有几个不正确的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) ; 

我怀疑是一个返回值的锁搞砸栈,基本上结束了错误的数据SetSystemTime函数。

2
 

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); 
 

这使用它在当地时区正常工作。

1
[DllImport("Kernel32.dll")] 
public static extern bool SetLocalTime(ref StractData.Datetime.SYSTEMTIME Time); 
0

你只是一个权限问题,当我运行该程序没有特权,它一点儿也不更改日期,但如果我做节目点击右键,然后点击以管理员身份运行,它的作品!

2

无需P/Invoke - Microsoft.VisualBasic程序集提供了一种更简单的方法(但不是众所周知的)。如果你在C#中,只要记得给它添加一个引用。可以使用Microsoft.VisualBasic.DateAndTime类来获取和更改日期或时间。属性TodayTimeOfDay有设置器,它们会对日期或时间进行更改。

您仍然需要相关权限 - 请参阅下面的MSDN文档链接。

下面是一个任意例如更改时间:MSDN上

public static void SetTimeToPast() 
{ 
    Microsoft.VisualBasic.DateAndTime.TimeOfDay = DateTime.Now.AddMinutes(-2); 
} 

参考:http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.dateandtime(v=vs.110).aspx