Showing ClickOnce deployment version on WPF application

后端 未结 5 958
夕颜
夕颜 2021-02-13 18:00

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

相关标签:
5条回答
  • 2021-02-13 18:14
    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'.

    0 讨论(0)
  • 2021-02-13 18:15

    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?

    0 讨论(0)
  • 2021-02-13 18:16

    This solution is similar to @Engin, but uses XPath.

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("...");
    XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);
    ns.AddNamespace("asmv1", "urn:schemas-microsoft-com:asm.v1");
    string xPath = "/asmv1:assembly/asmv1:assemblyIdentity/@version";
    XmlNode node = xmlDoc.SelectSingleNode(xPath, ns);
    string version = node.Value;
    
    0 讨论(0)
  • 2021-02-13 18:22

    OK, I found the problem. I had to add reference to System.Deployment That is why I couldn't use it. This dll is for winforms also.

    0 讨论(0)
  • 2021-02-13 18:38

    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);
    }
    
    0 讨论(0)
提交回复
热议问题