Reputation: 170489
My code on Windows used P/Invoke to call GetTickCount64()
from kernel32.dll This doesn't work under Mono on Ubuntu Server 14 - I get EntryPointNotFoundException
.
There's this approach using PerformanceCounter
. It works on Windows but when I use it on Linux with counter name/category set to System
/System Up Time
it yields zero timespans all the time. So it doesn't work too.
Environment.Tickount
works the same as on Windows but it overflows every 47 days and this can get confusing results for my usecase, so I'd prefer a 64-bit counter.
How do I get system uptime in C# code running with Mono on Ubuntu Server?
Upvotes: 0
Views: 768
Reputation: 170489
User Lex Li linked to this closely related question for C code.
Among the answers is this one which suggets reading from /proc/uptime
. This is just great for C# code - it can use File.ReadAllText()
and then parse the resulting string.
var uptimeText = File.ReadAllText( "/proc/uptime" );
// Now split the string to extract the first component,
// parse it as double and use TimeSpan.FromSeconds()
Upvotes: 1