0

Is there a way to tell when my application starts up if it was launched with Application.Restart()? I only use it when my application updates itself automatically and it would be helpful to know when my application is starting after an update.

Fr33dan
  • 4,227
  • 3
  • 35
  • 62
  • 1
    Well according to [this](http://stackoverflow.com/questions/95098/why-is-application-restart-not-reliable) and [this](http://stackoverflow.com/questions/779405/how-do-i-restart-my-c-sharp-winform-application) it's best to make a separate process and not use that method because it is unrelaiable. You can then throw in some command lines that let you know the process was initialized through your code. – chancea Dec 18 '14 at 15:13
  • Both questions route back to the same problem, and the failure of Application.Restart was only a symptom of another error in their code. There is nothing wrong with Application.Restart – Fr33dan Dec 18 '14 at 15:21
  • Hmm that's that I get for skim reading..at least they were relevant links..haha... – chancea Dec 18 '14 at 15:26

1 Answers1

3

There's no way you could do this out of the box. Two possible solutions come to my mind:

  1. Use a user setting to remember the state. For example, call it RestartedOnUpdate and set it to true before calling Application.Restart(). Set it to false after you've restarted.
  2. Instead of using Application.Restart() you could use Process.Start() and supply some kind of command line argument (like /updated).

Actually it is good practise to perform a settings update after doing a ClickOnce update, so that user settings that were changed are not reset to their default values.

The general approach to that is:

  1. Create a setting like SettingsUpgradeNeeded and set it to true in the designer. It will then be published like that to the client.
  2. In the Main method check whether the setting is true and perform a settings upgrade.
  3. Set the setting to false so no upgrade happens next run.

You could use that flag to check whether you started for the first time after a ClickOnce update.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • These are both the solutions I had in mind already if the answer was "there's no way out of the box". – Fr33dan Dec 18 '14 at 15:13
  • Personally, I'd go for the first one. The second one *does* have the advantage of not having to use `Application.Restart()`, on the other hand `Application.Restart()` will pass all the command line options to the application that were passed to it in the first place, which may come in handy. – Thorsten Dittmar Dec 18 '14 at 15:14
  • Unfortunately, they [won't in my context](http://stackoverflow.com/a/8870810/1111886), but I still prefer the first solution as well. – Fr33dan Dec 18 '14 at 15:16
  • @Fr33dan Talking about ClickOnce updates, I updated my answer, because you may already have the required functionality. – Thorsten Dittmar Dec 18 '14 at 15:19