How to restart WPF application after it has been updated using click-once, i need to start the new version!
-
a little more context? do you mean automatically? remotely? … – madd0 Mar 13 '11 at 20:44
-
1For such a scenario, configure the Check-for-Update to happen _before_ the app starts. It's an option under Publish. – H H Mar 13 '11 at 22:50
-
@madd0: from code, after click-once update installed using .Net Deployment API (fro Application.Deployment). – Saw Mar 14 '11 at 08:38
-
@Henk Holterman: but i've developed a custom deployment in the application it self, so i can't rely on the auto update before the app starts. – Saw Mar 14 '11 at 08:40
-
1have you tried to Process.Start the new version just before exiting the running version? – madd0 Mar 14 '11 at 09:02
6 Answers
There are a few ways but most don't work correctly, they end up reopening the old version.
It's going to sound crazy that WPF doesn't have a proper way to handle it (#fixwpf), but you'll need to reference System.Windows.Forms.dll
and call System.Windows.Forms.Application.Restart();
A quick search found Rob Relyea's post about the same thing (XAML, WPF Microsoft Guy) http://robrelyea.wordpress.com/2007/07/24/application-restart-for-wpf/
It isn't necessary to include the winforms assembly just for this, that seems like overkill.
You can do the same thing that winforms does behind the scenes in it's restart method. After the Update Has been applied:
String ApplicationEntryPoint = ApplicationDeployment.CurrentDeployment.UpdatedApplicationFullName;
Process.Start(ApplicationEntryPoint);
//shutdown current instance here
This Will Start the New Version Of Your Application With the proper ClickOnce initialization.

- 346
- 2
- 7
-
3For some reason, I had to use `String ApplicationEntryPoint = ApplicationDeployment.CurrentDeployment.UpdateLocation.AbsoluteUri;` – Carlos Blanco Jan 27 '14 at 18:04
-
1Works as long as your default browser handles clickonce deployments. Chrome doesn't seem to work in my case, but great alternative nonetheless. – reckface Apr 17 '15 at 08:07
-
Chrome doesn't seem to handle clickonce deployments, it shows up a Keep/Discard window. – abhilash Jul 27 '15 at 06:19
private static void RestartClickOnceApplication()
{
try
{
XDocument xDocument;
using (MemoryStream memoryStream = new MemoryStream(AppDomain.CurrentDomain.ActivationContext.DeploymentManifestBytes))
using (XmlTextReader xmlTextReader = new XmlTextReader(memoryStream))
{
xDocument = XDocument.Load(xmlTextReader);
}
var description = xDocument.Root.Elements().Where(p => p.Name.LocalName == "description").First();
var publisher = description.Attributes().Where(a => a.Name.LocalName == "publisher").First();
var product = description.Attributes().Where(a => a.Name.LocalName == "product").First();
var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu) + @"\Programs\";
path += publisher.Value + @"\" + product.Value + ".appref-ms";
if (File.Exists(path))
{
Process.Start(path);
Application.Current.Shutdown();
}
else
{
Application.Current.Shutdown();
}
}
catch
{
Application.Current.Shutdown();
}
}

- 228
- 3
- 9
-
Thx! I changed this solution a bit to make it work correctly for me. `var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Programs", publisher.Value, product.Value, product.Value + ".appref-ms");` – Витёк Синёв Apr 05 '19 at 20:49
-
@ВитёкСинёв yes. Path.Combine is better. Also Where is unnecessery in this code replace Where with First and delete First at the end. description.Attributes().Where(a => a.Name.LocalName == "publisher").First(); – Erhan Urun May 29 '23 at 14:25
Using what Michael provided:
String ApplicationEntryPoint = ApplicationDeployment.CurrentDeployment.UpdatedApplicationFullName;
Process.Start(ApplicationEntryPoint);
Does indeed have the problem of browsers not handling it correctly. For instance Edge would leave a blank browser page after opening your app. Because ApplicationDeployment.CurrentDeployment.UpdatedApplicationFullName
refers to a long http url address, there is also the theoretical chance that your Internet drops out the split second after the download finishes and thus your app will not get restarted (cannot access the url).
I went for this instead:
... Update()
if (System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\Programs\\MyCompany\\MyApp.appref-ms"))
{
System.Diagnostics.Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\Programs\\MyCompany\\MyApp.appref-ms");
}
else if (System.IO.File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\MyApp.appref-ms"))
{
System.Diagnostics.Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\MyApp.appref-ms");
}
else throw new InvalidOperationException("Cannot restart the application, because StartMenu and Desktop shortcuts are missing!");
... shut down application (this.Close() etc.)
This does of course assume, that you specified your ClickOnce deployment to create shortcuts and that no one has deleted them. But the chance of that is pretty low. (The user probably couldn't execute your app without those shortcuts, because ClickOnce deploys the .exe to a very buried location)
If you really-really wanted to, you could in the final else statement, instead of throwing an exception, create an appref-ms
file (google will help) in the temp directory and execute that.

- 2,266
- 4
- 27
- 48
Once you've started your application (Double-Clicked the .application file, that is) you won't notice automatically, since one thing the framework does for you at startup only is to check whether your local version is older than the one in the download site of the app.
But you can use the ApplicationDeployment-Class to check for updates, it has all means necessary IIRC.

- 4,095
- 3
- 39
- 60
right click on references in solution explorer > click add reference > click on assemblies > search and add System.Windows.Forms > in MainWindow add "System.Windows.Forms.Application.Restart();".
Done!

- 13
- 3