How to know when was Windows started or shutdown?

前端 未结 7 694
粉色の甜心
粉色の甜心 2020-12-10 05:12

I need to develop a program in C# find out when was Windows started or shutdown.

Is there a log file that I can read to know Windows start and shutdown times? Or do

7条回答
  •  轮回少年
    2020-12-10 05:35

    You could use the "System Up Time" performance counter to get the start time of the system:

     PerformanceCounter systemUpTime = new PerformanceCounter("System", "System Up Time");
    
     systemUpTime.NextValue();
     TimeSpan upTimeSpan = TimeSpan.FromSeconds(systemUpTime.NextValue());
     Console.Out.WriteLine(DateTime.Now.Subtract(upTimeSpan).ToShortTimeString());
    

    Hope, this helps.

提交回复
热议问题