Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing ClickOnce deployment version on WPF application

I'm deploying now a WPF c# project and want to put the clickonce version (rather than the assembly or product version) on the screen title. I used to do it in Win form application in the following way. But it seems that it is not the way in WPF applications. I searched on Google but didn't find anything. Please help.

    if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
    {
        ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
        lblVer.Text = "V" + ad.CurrentVersion.ToString();
    }
    else
        lblVer.Text = "V" + Application.ProductVersion.ToString();
like image 313
Igal Avatar asked Jan 04 '11 07:01

Igal


3 Answers

Try this:

public static Version GetPublishedVersion()
{
    XmlDocument xmlDoc = new XmlDocument();
    Assembly asmCurrent = System.Reflection.Assembly.GetExecutingAssembly();
    string executePath = new Uri(asmCurrent.GetName().CodeBase).LocalPath;

    xmlDoc.Load(executePath + ".manifest");
    string retval = string.Empty;
    if (xmlDoc.HasChildNodes)
    {
        retval = xmlDoc.ChildNodes[1].ChildNodes[0].Attributes.GetNamedItem("version").Value.ToString();
    }
    return new Version(retval);
}
like image 186
Engin Ardıç Avatar answered Nov 01 '22 06:11

Engin Ardıç


What error do you get? There's no difference in the ClickOnce API's between Windows Forms and WPF. It is not dependent upon any UI framework.

Did you remember to add a reference to System.Deployment.dll?

like image 6
Josh Avatar answered Nov 01 '22 07:11

Josh


using System;
using System.Deployment.Application;

namespace Utils
{
    public class ClickOnce
    {
        public static Version GetPublishedVersion()
        {
            return ApplicationDeployment.IsNetworkDeployed 
                ? ApplicationDeployment.CurrentDeployment.CurrentVersion 
                : System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
        }
    }
}

If you get an error about System.Deployment.Application, then Solution > Project > References > Add Reference > Assemblies > Framework > System.Deployment.

Do not parse the assembly XML for this information; you're relying on undocumented behaviour which simply happens to work 'for now'.

like image 3
Doug Avatar answered Nov 01 '22 05:11

Doug