Yoopergeek
Yoopergeek

Reputation: 5642

How to restart my application if Windows Update forces a reboot?

At the office, when I leave for the night I very rarely log off or reboot. I simply lock my workstation and go home, leaving all my development tools exactly how I left them.

If Windows-Update rolls through and reboots my machine in the middle of the night I'm only slightly peeved because when I log back in the next morning, any MS Office application, or Visual Studio instance I had running will have already automatically restarted, opening whatever file(s)/projects/solutions I may have been working on.

My question is: How can I make my Windows Forms applications (C#) do this? Is there some way for my application to "register" that it wants to be restarted if the system automatically reboots?

Upvotes: 15

Views: 2345

Answers (4)

Thomas Levesque
Thomas Levesque

Reputation: 292555

A simple way is to add an entry to the following registry key :

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce

Just create a value containing the path of your app (optionally including command line arguments). The app will be run at the next startup, then the value will be deleted.

Upvotes: 2

Brian
Brian

Reputation: 25834

Step 1: Figure out a way to differentiate a windows-triggered restart from a standard one. One solution would be to try preprocessing messages. They're probably different for a windows-triggered restart...or at least they are in Vista in some cases :/

Step 2: If you detect it's a windows-triggered restart, add a scheduled, one-time task.

Upvotes: 0

Jacob Adams
Jacob Adams

Reputation: 4004

If you have Windows Vista or Windows 7, you can use the Managed Restart and Recovery API. The links on that page also point to some useful blog entries

http://channel9.msdn.com/posts/DanielMoth/Windows-Vista-Restart-amp-Recovery-APIs-from-managed-code/

Upvotes: 4

Rob
Rob

Reputation: 45780

I think the RegisterApplicationRestart Win32 API function might be what you're after, it's part of the Restart Manager API.

Upvotes: 9

Related Questions