You can create a flag in application settings and then process it on startup:
//Set a flag on restart
Properties.Settings.Default.IsRestarting = true;
Properties.Settings.Default.Save();
Application.Restart();
...
//Process it on startup (e.g. Main method or Form.Load event for the main form)
if(Properties.Settings.Default.IsRestarting)
{
// run some commands and change some variables here
Properties.Settings.Default.IsRestarting = false;
Properties.Settings.Default.Save();
}
Alternatively, you can store this flag anywhere you want: file system, registry, database, etc.
You can also, use Process.Start
instead of Application.Restart
to run another instance of your app. This way you can interact with a new process: send command line arguments, pass messages.
Also, see: Why is Application.Restart() not reliable?