Environment.TickCount is not enough

后端 未结 6 1067
别那么骄傲
别那么骄傲 2021-01-13 11:50

I want to know on when was the last time the system was started.

Environment.TickCount will work but it is breaking after 48-49 days because of the limitation of int

6条回答
  •  南方客
    南方客 (楼主)
    2021-01-13 12:15

    public void BootTime(){    
        SelectQuery query = new SelectQuery("SELECT LastBootUpTime FROM Win32_OperatingSystem WHERE Primary='true'");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    
        foreach (ManagementObject mo in searcher.Get())
        {
            DateTime dtBootTime = ManagementDateTimeConverter.ToDateTime(mo.Properties["LastBootUpTime"].Value.ToString());
            Console.WriteLine(dtBootTime.ToString());
        }
    }
    

提交回复
热议问题