在WPF应用程序中显示ClickOnce部署版本

14

我现在正在部署一个WPF c#项目,想将 clickonce 版本 (而不是程序集或产品版本)放在屏幕标题上。 我曾经在Win表单应用程序中以以下方式完成。 但似乎这不是WPF应用程序的方法。 我在Google上搜索了一下,但没有找到任何信息。 请帮忙。

    if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
    {
        ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
        lblVer.Text = "V" + ad.CurrentVersion.ToString();
    }
    else
        lblVer.Text = "V" + Application.ProductVersion.ToString();
5个回答

26

试试这个:

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

6
你遇到了什么错误?在Windows Forms和WPF之间的ClickOnce API没有任何区别。它不依赖于任何UI框架。
你是否记得添加对System.Deployment.dll的引用?

3
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;
        }
    }
}

如果您收到有关 System.Deployment.Application 的错误,则需要执行以下步骤:Solution > Project > References > Add Reference > Assemblies > Framework > System.Deployment。

请勿解析此信息的程序集 XML;您正在依赖于未记录的行为,它仅恰好“现在”可用。

3

好的,我找到了问题所在。 我需要添加对System.Deployment的引用, 这就是为什么我不能使用它。这个dll也适用于winforms。


0

这个解决方案与@Engin类似,但使用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;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接