问题描述:

How to restart WPF application after it has been updated using click-once, i need to start the new version!

网友答案:

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.

网友答案:

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.

网友答案:

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.

网友答案:

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!

相关阅读:
Top