简体   繁体   English

如何判断 windows 是否重启?

[英]How to determine if windows restarted?

I want my WPF application run just one time.我希望我的 WPF 应用程序只运行一次。 I have no problem with this.我对此没有任何问题。
My problem is how can I determine if windows currently restarted?我的问题是如何确定 windows 当前是否重新启动?

You could write a file to disk, and then immediately mark it as 'delete on reboot' using MoveFileEx:您可以将文件写入磁盘,然后立即使用 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# 使用 C# 检索系统正常运行时间

Some psudeocode:一些伪代码:

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

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

相关问题 如何在计算机重新启动后保持 Windows 服务运行? - How to keep Windows Service running after computer is restarted? 重新启动另一个Windows服务时,Windows服务如何以编程方式自行重启? - How can a windows service programmatically restart itself when another windows service is restarted? 是否为C#Windows服务提供OnRestart()事件侦听器? 如何查看Windows服务是否已重新启动? - Is there a OnRestart() event listener for a C# Windows service? How can I see if the Windows Service was Restarted? 如何确定Windows是哪个版本? - How to determine which version of Windows? 在Windows 8中的来宾帐户中如何确定Windows版本? - IN guest account in windows 8 how to determine the version of windows? Windows 服务如何确定其服务名称? - How can a Windows Service determine its ServiceName? 如何确定 Windows 应用程序是否在屏幕外? - How to determine whether a Windows application is offscreen? 如何确定Windows窗体中的文本框是否具有焦点 - How to determine if a textbox in a windows form has focus 如何确定当前Windows会话是否被锁定? - How to determine if the current Windows session is locked or not? 如何确定Windows域中的计算机是否处于“活动”状态 - How to determine if a computer in my Windows Domain is “active”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM