【发布时间】:2015-11-04 04:02:58
【问题描述】:
如何以编程方式更改 BIOS 时间设置?该代码将包含在 C# Window Forms 应用程序中,以确保 BIOS 设置始终为 UTC 时间。我尝试使用 Win32_UTCTime 在 WMI 中找到解决方案,但失败了。
【问题讨论】:
如何以编程方式更改 BIOS 时间设置?该代码将包含在 C# Window Forms 应用程序中,以确保 BIOS 设置始终为 UTC 时间。我尝试使用 Win32_UTCTime 在 WMI 中找到解决方案,但失败了。
【问题讨论】:
在更改/操作 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();
}
}
【讨论】:
您无法通过任何正常方式以编程方式更改 BIOS 时间。
BIOS 存储在独立于操作系统的 EEPROM 中。与之交互的唯一方法是通过直接写入硬件的直接程序。各种 API 不提供执行此操作的方法。
【讨论】: