简体   繁体   中英

How to determine if windows restarted?

I want my WPF application run just one time. I have no problem with this.
My problem is how can I determine if windows currently restarted?

You could write a file to disk, and then immediately mark it as 'delete on reboot' using MoveFileEx:

So in psuedocode:

if(File.Exists(CheckFileName))
   return false; // already ran once
else {
   // write out the check file
   using(checkFile = File.Create(CheckFileName, ...)) {
      // and mark it as delete on boot
      MoveFileEx(checkFile.SafeHandle,
         null,
         MOVEFILE_DELAY_UNTIL_REBOOT);
      return true; // ok to run
   }
} 

You could check and store the system uptime along with the last runtime and compare that with the current uptime.

Retrieve system uptime using C#

Some psudeocode:

   DateTime computerLastStarted = Now - Uptime;
   if (computerLastStarted > storedComputerLastStarted + or - tollerance) {
      storedComputerLastStarted = computerLastStarted;
      StartProgram();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM