You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C#程序中获取任务管理器中的最后BIOS时间?

获取任务管理器中的BIOS时间(C#实现)

关于存储位置

该信息不直接存储在注册表中,需要通过系统提供的WMI接口或Windows API来获取。

实现方法

任务管理器中显示的“BIOS时间”本质是系统完成BIOS自检后启动操作系统的时间,以下是两种可行的实现方式:


方法一:使用WMI查询

通过Win32_OperatingSystem类的LastBootUpTime属性获取,该属性返回的系统启动时间戳与任务管理器显示的BIOS时间一致。

代码示例:

using System;
using System.Management;

public class BiosBootTimeReader
{
    public static DateTime GetBiosBootTime()
    {
        using (var searcher = new ManagementObjectSearcher("SELECT LastBootUpTime FROM Win32_OperatingSystem"))
        {
            foreach (ManagementObject obj in searcher.Get())
            {
                string rawBootTime = obj["LastBootUpTime"].ToString();
                return ManagementDateTimeConverter.ToDateTime(rawBootTime);
            }
        }
        throw new InvalidOperationException("无法获取BIOS启动时间");
    }

    public static void Main()
    {
        try
        {
            DateTime bootTime = GetBiosBootTime();
            Console.WriteLine($"BIOS启动时间:{bootTime:yyyy-MM-dd HH:mm:ss}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"获取失败:{ex.Message}");
        }
    }
}

注意:需要在项目中添加System.Management程序集引用。


方法二:使用Windows API(P/Invoke)

通过GetTickCount64获取系统已运行的毫秒数,结合当前时间反向计算出启动时间,这种方式无需额外引用程序集,更轻量。

代码示例:

using System;
using System.Runtime.InteropServices;

public class BiosBootTimeReader
{
    [DllImport("kernel32.dll")]
    private static extern ulong GetTickCount64();

    public static DateTime GetBiosBootTime()
    {
        ulong uptimeMs = GetTickCount64();
        return DateTime.Now.Subtract(TimeSpan.FromMilliseconds(uptimeMs));
    }

    public static void Main()
    {
        DateTime bootTime = GetBiosBootTime();
        Console.WriteLine($"BIOS启动时间:{bootTime:yyyy-MM-dd HH:mm:ss}");
    }
}

内容的提问来源于stack exchange,提问作者jebac pis

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 04:16:18