Restart WPF application after click-once update (start the new version)

早过忘川 提交于 2019-12-17 22:39:16

问题


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


回答1:


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/




回答2:


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.




回答3:


        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();
            }
        }



回答4:


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.




回答5:


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.




回答6:


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!



来源:https://stackoverflow.com/questions/5291937/restart-wpf-application-after-click-once-update-start-the-new-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!