[英]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.