如何在后面的代码中阅读WPF发布版本号

时间:2021-11-10 15:42:33

I want to read and display WPF application publish version number in splash windows, In project properties in publish tab there is publish version, how can I get this and display it in WPF windows.

我想在启动窗口中阅读并显示WPF应用程序发布版本号,在发布选项卡中的项目属性中有发布版本,我该如何获取它并在WPF窗口中显示它。

Thanks in advance

提前致谢

4 个解决方案

#1


12  

Add reference to System.Deployment library to your project and adjust this snippet to your code:

将System.Deployment库的引用添加到项目中,并将此代码段调整为您的代码:

using System.Deployment.Application;

and

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

As stated in comment, you cannot obtain publish version during debug, so I suggest to handle InvalidDeploymentException.

如评论中所述,您无法在调试期间获取发布版本,因此我建议处理InvalidDeploymentException。

#2


22  

Access the assembly version using Assembly.GetExecutingAssembly() and display in UI

使用Assembly.GetExecutingAssembly()访问程序集版本并在UI中显示

Assembly.GetExecutingAssembly().GetName().Version.ToString();

#3


3  

string version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); 

Console.WriteLine(version);

#4


1  

use System.Reflection.Assembly.GetEntryAssembly().GetName().Version to avoid get version from dll, will get always the version from the current "exe".

使用System.Reflection.Assembly.GetEntryAssembly()。GetName()。版本以避免从dll获取版本,将始终获得当前“exe”的版本。

#1


12  

Add reference to System.Deployment library to your project and adjust this snippet to your code:

将System.Deployment库的引用添加到项目中,并将此代码段调整为您的代码:

using System.Deployment.Application;

and

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

As stated in comment, you cannot obtain publish version during debug, so I suggest to handle InvalidDeploymentException.

如评论中所述,您无法在调试期间获取发布版本,因此我建议处理InvalidDeploymentException。

#2


22  

Access the assembly version using Assembly.GetExecutingAssembly() and display in UI

使用Assembly.GetExecutingAssembly()访问程序集版本并在UI中显示

Assembly.GetExecutingAssembly().GetName().Version.ToString();

#3


3  

string version = Assembly.GetExecutingAssembly().GetName().Version.ToString(); 

Console.WriteLine(version);

#4


1  

use System.Reflection.Assembly.GetEntryAssembly().GetName().Version to avoid get version from dll, will get always the version from the current "exe".

使用System.Reflection.Assembly.GetEntryAssembly()。GetName()。版本以避免从dll获取版本,将始终获得当前“exe”的版本。