Home > Software design >  How to get last system shutdown\boot time (Win10)
How to get last system shutdown\boot time (Win10)

Time:10-15

I try to get last system shutdown and\or reboot. But it not works for me.

I make reboot my machine yeasterday and make shutdown-power on 10 minutes ago. But code says me about last shutdown - it is yeasterday.

I read this questions:

Get the date-time of last windows shutdown event using .NET

How to know when was Windows started or shutdown?

But it is not work for my machine.

I try this code:

  var _rebootPerformanceCounter = new global::System.Diagnostics.PerformanceCounter("System", "System Up Time");
   _rebootPerformanceCounter.NextValue(); 
        var uptimeSpan = TimeSpan.FromSeconds(_rebootPerformanceCounter.NextValue());
        var uptime = uptimeSpan.ToString();

And this:

  string sKey = @"System\CurrentControlSet\Control\Windows";
        using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(sKey))
        {
            string sValueName = "ShutdownTime";
            byte[] val = (byte[]) key.GetValue(sValueName);
            long valueAsLong = BitConverter.ToInt64(val, 0);
            var shutdownTime= DateTime.FromFileTime(valueAsLong);


            Console.WriteLine(shutdownTime); //THIS             
        }

And this:

   if (EventLog.Exists("System"))
        {
            var log = new EventLog("System", Environment.MachineName, "EventLog");

            var entries = new EventLogEntry[log.Entries.Count];
            log.Entries.CopyTo(entries, 0);

            var startupTimes = entries.Where(x => x.InstanceId == 2147489653).Select(x => x.TimeGenerated);
            var shutdownTimes = entries.Where(x => x.InstanceId == 2147489654).Select(x => x.TimeGenerated);
            var shutdownEvents = entries.Where(x => x.InstanceId == 2147484722);
        }

So, the values are same: it says that i reboot my machine last evening and do not show last power off time. I try this code on PC and notebook with same results. At my PC i have uninterrupted power supply unit and i turned it off.

So I'm confused. What am I doing wrong?

CodePudding user response:

Solution is very simple: i should off "clever power management" with -

 powercfg -h off 

And code works!

  • Related