开发者

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

How to restart WPF appli开发者_如何学JAVAcation 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.


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


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.


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.


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜