繁体   English   中英

如何判断 windows 是否重启?

[英]How to determine if windows restarted?

我希望我的 WPF 应用程序只运行一次。 我对此没有任何问题。
我的问题是如何确定 windows 当前是否重新启动?

您可以将文件写入磁盘,然后立即使用 MoveFileEx 将其标记为“重新启动时删除”:

所以在伪代码中:

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
   }
} 

您可以检查并存储系统正常运行时间以及上次运行时间,并将其与当前正常运行时间进行比较。

使用 C# 检索系统正常运行时间

一些伪代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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