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

我现在正在部署一个WPF c#项目,并希望将clickonce版本 (而不是assymbly或产品版本)放在屏幕标题上。 我过去常常在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(); 

试试这个:

 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 Forms和WPF之间的ClickOnce API没有区别。 它不依赖于任何UI框架。

你还记得添加对System.Deployment.dll的引用吗?

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

 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以获取此信息; 你依赖的是无证的行为,这种行为恰好是“现在”。

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