获取上次重启时间 [英] Getting last reboot time
问题描述
可能重复:
显示构建日期
如何知道Windows何时启动或关闭?
出于我的目的,我正在编写一个C#可执行文件,该文件将计算从当前时间到上次重新引导服务器的时间之间的时间差(分钟).
for my purposes I am writing a C# executable that will calculate the difference in time (minutes) from the time right now and the time the server was last rebooted.
我现在正在做的是捕获并解析来自cmd->"net stats server"的输出,并创建一个新的 DateTime
对象,然后将其与 DateTime.Now
和 TimeSpan
对象.
What I am currently doing now is capturing and parsing the output from cmd -> "net stats server" and creating a new DateTime
object then comparing that with DateTime.Now
with a TimeSpan
object.
是否有一种更干净的方式不使用第三方下载?我担心并不是"net stats服务器"中的所有日期格式都符合我的期望.
Is there a cleaner way to do this without the use of 3rd party downloads? I am scared that not all date formats from "net stats server" are in the format that I will expect.
**编辑我的问题,这是重复的,但是对于我的解决方案来说,值得使用的是它:
**edit my bad, this is a duplicate, but for what it is worth my solution was using this:
float ticks = System.Environment.TickCount;
Console.WriteLine("Time Difference (minutes): " + ticks / 1000 / 60);
Console.WriteLine("Time Difference (hours): " + ticks / 1000 / 60 / 60);
Console.WriteLine("Time Difference (days): " + ticks / 1000 / 60 / 60 / 24);
推荐答案
此答案应该可以为您提供帮助.如果您想知道系统上次重新启动的时间,只需取正常运行时间值并将其从当前日期/时间中减去
this answer should help you. If you want to know when the system was last rebooted just take the uptime value and subtract it from the current date/time
链接的答案中的代码
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());
}
}
}
这篇关于获取上次重启时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!