Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
I am reading LastBootUpTime by following method but it displays few days old time while I shutdown my PC on daily basis. I know the solution described in this link
Windows 10 LastBootUpTime not updating[^] but enforce each user to change system setting is not possible. So what will be the better solution to read LastBootUpTime.

Please guide

What I have tried:

C#
ManagementScope scope = new ManagementScope(@"root\cimv2");

// Create a query to get the build number.
ObjectQuery query = new ObjectQuery("SELECT LastBootUpTime FROM Win32_OperatingSystem");

// Execute the query.
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, queryObj);
ManagementObjectCollection queryCollection = searcher.Get();

string lastBootupTime="";
foreach (ManagementObject m in queryCollection)
{
 lastBootupTime= = ManagementDateTimeConverter.ToDateTime(mo["LastBootUpTime"].ToString());
 break;
}
Posted
Updated 30-Dec-22 3:02am
v2
Comments
0x01AA 30-Dec-22 9:07am    
Just a guess, maybe from the windows events: Eventvwr -> System -> Event-ID == 12 ?
Dave Kreskowiak 30-Dec-22 10:42am    
The LastBootUpTime you are getting from Win32_OperatingSystem is accurate. It's your interpretation of the data that needs work. Like 'Griff said below, you have to understand what Windows considers a "shutdown" and "boot", and what is actually happening when you execute those.
DoingWork 10-Jan-23 0:37am    
Yes. It means there should be another time LastShutDownTime but seems it is still not available.
Dave Kreskowiak 10-Jan-23 10:42am    
There is no LastShutDownTime anywhere. I have no idea what you're talking about.

The last boot time is probably correct - it's just your idea of what you do with your computer that differs from Windows. With Win 10 / 11 (and maybe earlier, I'm not sure) "shutdown" and "restart" don't do what the name implies, not really - your computer probably doesn't do a full boot but loads from a app free "image" of your OS when you told it to shutdown - it's not the same as a full reboot!

And worse, there are Sleep and Hibernate as well, which don't even pretend to do a restart - and these are often mapped to the "powerdown" button.

My system for example is currently running with an uptime of 14 days, 22 hours, 58 minutes, and 11 seconds which is cumulative across the best part of a couple of months.
 
Share this answer
 
Comments
DoingWork 10-Jan-23 0:38am    
Yes. 100 Percent agreed with your comment. But now searching for LastShutdownTime but perhaps it is still not provided by Windows OS.
You could write a windows service[^] that stores the date & time every minute in a specific file. Then your reporting app can use that information.

UPDATE
Maybe you are looking for Logged in/out session information. This may help: How to get Current users login and logoff time in system?[^]
 
Share this answer
 
v3
Comments
0x01AA 30-Dec-22 11:15am    
I think, not really a solution for what OP is looking for, but also not deserve a 1. That's Why my small 5 to adjust a little bit;)
Graeme_Grant 30-Dec-22 11:41am    
Thanks for the vote. I was offering an alternative to using LastBootUpTime. I should have expanded my answer, however, OriginalGiff has done that.
DoingWork 10-Jan-23 0:41am    
There should be also LastShutdownTime. However UserLoginTime has solved my problem.
Graeme_Grant 10-Jan-23 0:43am    
Glad to hear that you found a solution that works for you. 😊

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900