我必须使用某种技术来获取System Up时间。由StackOverflow中的某个人推荐我已经使用Performance Counter来获取TimeSpan中的System Up时间。PerformanceCounter抛出异常
try
{
var uptime = new PerformanceCounter("System", "System Up Time");
uptime.NextValue();
return TimeSpan.FromSeconds(uptime.NextValue());
}
catch (Exception)
{
}
它在我的电脑上完美运行没有任何问题。但问题发生在我朋友的电脑中。在他的PC上,当运行下面的行时会引发异常。
var uptime = new PerformanceCounter("System", "System Up Time");
例外是"Input String was not in Correct format"
。
区域设置问题? – Guillaume 2012-03-06 13:42:46
不,PerformanceCounter不会引发FormatException。考虑删除try/catch关键字。 – 2012-03-06 13:45:48
@HansPassant,我也相信,直到我发现这个异常,我试图删除try/catch,我的应用程序冻结,它说,“没有反应”.... – jchoudhury 2012-03-06 13:55:39