【问题标题】:Showing ClickOnce deployment version on WPF application在 WPF 应用程序上显示 ClickOnce 部署版本
【发布时间】:2011-06-03 05:53:05
【问题描述】:
我现在正在部署一个 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();
【问题讨论】:
标签:
c#
wpf
deployment
clickonce
【解决方案1】:
好的,
我发现了问题。
我必须添加对System.Deployment 的引用
这就是为什么我不能使用它。此 dll 也适用于 winforms。
【解决方案2】:
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 以获取此信息;您依赖的无证行为只是碰巧“暂时”起作用。
【解决方案3】:
此解决方案类似于@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;
【解决方案4】:
试试这个:
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);
}
【解决方案5】:
你得到什么错误? Windows 窗体和 WPF 之间的 ClickOnce API 没有区别。它不依赖于任何 UI 框架。
您还记得添加对 System.Deployment.dll 的引用吗?