找到自Windows启动以来经过的时间的适当功能?

时间:2009-02-09 12:36:45

标签: .net-2.0

我知道Environment.TickCount存在,但我有一个很大的问题,它只限于25天。

什么是不需要外部模块/ dll的好替代方案,并且可以在Windows Server 2003的vanilla安装中运行?

感谢

2 个答案:

答案 0 :(得分:1)

有很多选择。

public TimeSpan SystemUpTime()
{
    PerformanceCounter upTime = 
        new PerformanceCounter("System", "System Up Time");

    // You've got to call this twice. First time it returns 0 
    // and the second time it returns the real info.
    upTime.NextValue();   
    return TimeSpan.FromSeconds(upTime.NextValue());
}

但是,如果您没有足够的权限,则不会运行。

您也可以使用WMI

答案 1 :(得分:0)

您可以使用WMI获取WIN32_OperatingSystem.LastBootUpTime