【发布时间】:2014-06-21 00:10:18
【问题描述】:
我想在启动窗口中读取和显示 WPF 应用程序发布版本号,在发布选项卡中的项目属性中有发布版本,我怎样才能得到它并在 WPF 窗口中显示它。
提前致谢
【问题讨论】:
标签: c# wpf xaml code-behind
我想在启动窗口中读取和显示 WPF 应用程序发布版本号,在发布选项卡中的项目属性中有发布版本,我怎样才能得到它并在 WPF 窗口中显示它。
提前致谢
【问题讨论】:
标签: c# wpf xaml code-behind
使用Assembly.GetExecutingAssembly() 访问程序集版本并在 UI 中显示
Assembly.GetExecutingAssembly().GetName().Version.ToString();
【讨论】:
在您的项目中添加对System.Deployment 库的引用,并将此sn-p 调整为您的代码:
using System.Deployment.Application;
和
string version = null;
try
{
//// get deployment version
version = ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString();
}
catch (InvalidDeploymentException)
{
//// you cannot read publish version when app isn't installed
//// (e.g. during debug)
version = "not installed";
}
如评论所述,调试时无法获取发布版本,建议处理InvalidDeploymentException。
【讨论】:
使用GetEntryAssembly 而不是GetExecutingAssembly 从当前可执行文件而不是从当前执行的DLL 中获取版本,如下所示:
string version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString();
【讨论】:
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
Console.WriteLine(version);
【讨论】: