如何以编程方式更改BIOS时间设置?

问题描述:

如何以编程方式更改BIOS时间设置?该代码将被包含到C#Window Forms应用程序中,以确保BIOS设置始终为UTC时间。我尝试使用Win32_UTCTime在WMI中找到解决方案,但失败了。如何以编程方式更改BIOS时间设置?

更改/操作BIOS时钟之前read this article。它解释了为什么Windows依靠跟踪当地时间的时钟。所以你可能想远离改变它。

做一个实际的变化see this example from on PInvoke.NET

class Class1 
{ 
    /// <summary> This structure represents a date and time. </summary> 
    public struct SYSTEMTIME 
    { public ushort wYear,wMonth,wDayOfWeek,wDay, 
      wHour,wMinute,wSecond,wMilliseconds; 
    } 

    /// <summary> 
    /// This function retrieves the current system date 
    /// and time expressed in Coordinated Universal Time (UTC). 
    /// </summary> 
    /// <param name="lpSystemTime">[out] Pointer to a SYSTEMTIME structure to 
    /// receive the current system date and time.</param> 
    [DllImport("kernel32.dll")] 
    public extern static void GetSystemTime(ref SYSTEMTIME lpSystemTime); 

    /// <summary> 
    /// This function sets the current system date 
    /// and time expressed in Coordinated Universal Time (UTC). 
    /// </summary> 
    /// <param name="lpSystemTime">[in] Pointer to a SYSTEMTIME structure that 
    /// contains the current system date and time.</param> 
    [DllImport("kernel32.dll")] 
    public extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime); 

    static void Main() 
    { Console.WriteLine(DateTime.Now.ToString()); 
     SYSTEMTIME st = new SYSTEMTIME(); 
     GetSystemTime(ref st); 
     Console.WriteLine("Adding 1 hour..."); 
     st.wHour = (ushort)(st.wHour + 1 % 24); 
     if (SetSystemTime(ref st) == 0) 
      Console.WriteLine("FAILURE: SetSystemTime failed"); 
     Console.WriteLine(DateTime.Now.ToString()); 
     Console.WriteLine("Setting time back..."); 
     st.wHour = (ushort)(st.wHour - 1 % 24); 
     SetSystemTime(ref st); 
     Console.WriteLine(DateTime.Now.ToString()); 
     Console.WriteLine("Press Enter to exit"); 
     Console.Read(); 
    } 
} 
+0

据我了解,这种方法只修改系统时间,但BIOS时间仍然不变。我错了吗? – FMuk 2012-03-22 14:02:41

+0

在Windows中设置时间会更改我的计算机上的BIOS时间。 – 2012-03-22 14:39:43

+0

根据您提到的来源,有硬件实时时钟(我在BIOS帮助中设定时间将其命名为“BIOS时间”)和基于RTC的系统(Windows)时间,并在OS中指示移位,例如时区。 Windows API仅修改系统(OS)时间。 (对不起,如果我理解错了什么) – FMuk 2012-03-22 15:18:13

不能以编程方式更改BIOS的时间......通过任何正常的手段。

BIOS存储在独立于操作系统的EEPROM上。与之交互的唯一方法是通过直接写入硬件的直接程序。各种API不提供这样做的方法。