繁体   English   中英

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

[英]Showing ClickOnce deployment version on WPF application

我现在正在部署一个 WPF c# 项目,并希望将clickonce 版本(而不是程序集或产品版本)放在屏幕标题上。 我曾经通过以下方式在 Win 表单应用程序中执行此操作。 但在 WPF 应用程序中似乎不是这样。 我在谷歌上搜索,但没有找到任何东西。 请帮忙。

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

试试这个:

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

你得到什么错误? Windows 窗体和 WPF 之间的 ClickOnce API 没有区别。 它不依赖于任何 UI 框架。

您是否记得添加对 System.Deployment.dll 的引用?

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的错误,则解决方案 > 项目 > 参考 > 添加参考 > 程序集 > 框架 > System.Deployment。

不要来解析该信息汇编XML; 您依赖于无证行为,而这些行为恰好“暂时”起作用。

好的,我发现了问题。 我不得不添加对System.Deployment引用,这就是我不能使用它的原因。 这个dll也适用于winforms。

此解决方案类似于 @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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM