How to know when was Windows started or shutdown?

前端 未结 7 693
粉色の甜心
粉色の甜心 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:31

    System.Environment.TickCount has a 24.8 days limitation.
    This is because TickCount is a millisecond value contained in a signed 32 bits value.

    Windows API exposes these two functions:
    GetTickCount - returns a 32 bits value - available from Windows 2000
    GetTickCount64 - returns a 64 bits value - available from Vista/Windows Server 2008

    You can use GetTickCount64 this way:

    using System.Runtime.InteropServices;  
    
    [DllImport("Kernel32.dll")]  
    static extern long GetTickCount64();  
    
    DateTime osStartTime = DateTime.Now - new TimeSpan(10000 * GetTickCount64());
    

提交回复
热议问题