使用C#检索系统正常运行时间 [英] Retrieve system uptime using C#

查看:229
本文介绍了使用C#检索系统正常运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种简单的方法,使用C#来获取系统的正常运行时间?

解决方案

 公开时间跨度正常运行时间{
    得到 {
        使用(VAR运行时间=新的PerformanceCounter(系统,系统运行时间)){
            uptime.NextValue(); //读取它的值之前调用此额外的时间
            返回TimeSpan.FromSeconds(uptime.NextValue());
        }
    }
}
 

Is there a simple way to get a system's uptime using C#?

解决方案

public TimeSpan UpTime {
    get {
        using (var uptime = new PerformanceCounter("System", "System Up Time")) {
            uptime.NextValue();       //Call this an extra time before reading its value
            return TimeSpan.FromSeconds(uptime.NextValue());
        }
    }
}

这篇关于使用C#检索系统正常运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆