【问题标题】:How programmatically change BIOS time setting?如何以编程方式更改 BIOS 时间设置?
【发布时间】:2015-11-04 04:02:58
【问题描述】:

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

【问题讨论】:

    标签: winforms wmi bios


    【解决方案1】:

    在更改/操作 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 时间仍然保持不变。我错了吗?
    • 在 Windows 中设置时间会更改我机器上的 BIOS 时间。
    • 根据您所指的来源,有硬件实时时钟(我将其命名为“BIOS 时间”,因为它在 BIOS 帮助下设置时间)和基于 RTC 的系统(Windows)时间,并在操作系统中指示了班次,例如时区。 Windows API 仅修改系统 (OS) 时间。 (对不起,如果我理解错了)
    • 是的,你是对的 - 它改变了两者。重新启动后,系统保持更改的 RTC 时间,但系统恢复到以前的值。我不确定我是否理解它是如何发生的。无论如何,谢谢你。不幸的是,规范要求避免 Win API。
    • 也许 Windows 配置为与网络或 Internet 上的时钟同步。在尝试更改对系统如此重要的内容时避免 Win API 几乎是不可能的。
    【解决方案2】:

    您无法通过任何正常方式以编程方式更改 BIOS 时间。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      • 2015-04-09
      • 2011-01-12
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多