I have to get the System Up time using some sort of techniques. Suggested by someone in StackOverflow I have used Performance Counter to get the System Up time in TimeSpan.
try
{
var uptime = new PerformanceCounter("System", "System Up Time");
uptime.NextValue();
return TimeSpan.FromSeconds(uptime.NextValue());
}
catch (Exception)
{
}
It’s perfectly run on my PC without any problem. But problem occurs in my friend’s computer. On his PC, while running the following line throws exception.
var uptime = new PerformanceCounter("System", "System Up Time");
Exception was "Input String was not in Correct format".
PerformanceCountercan behave differently depending on permissions etc. – for some information regarding this see MSDN.Another point is that in Windows XP some performance counters are not available if no paging file is configured (for details see http://support.microsoft.com/kb/884558/en-us).
Something unrelated:
“Swallowing” exceptions like you do with an empty
catchblock is BAD practice!