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?
Asked
Active
Viewed 291 times
0

Navid Rahmani
- 7,848
- 9
- 39
- 57
-
2Are you wanting this WPF application to automatically restart when windows restarts? I think a little more information is needed on exactly what you are trying to do. – Jonathan Nixon Jun 28 '11 at 16:37
2 Answers
7
You could write a file to disk, and then immediately mark it as 'delete on reboot' using MoveFileEx:
- MSDN: http://msdn.microsoft.com/en-us/library/aa365240(v=VS.85).aspx
- Pinvoke.net: http://www.pinvoke.net/default.aspx/kernel32.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
}
}

Ryan
- 606
- 5
- 12
4
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();
}

Community
- 1
- 1

Daniel A. White
- 187,200
- 47
- 362
- 445