簡體   English   中英

如何在代碼后面閱讀WPF發布版本號

[英]How can I read WPF publish version number in code behind

我想在啟動窗口中閱讀和顯示WPF應用程序發布版本號,在發布選項卡中的項目屬性中有發布版本,我該如何獲取它並在WPF窗口中顯示它。

提前致謝

使用Assembly.GetExecutingAssembly()訪問程序集版本並在UI中顯示

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

System.Deployment庫的引用添加到項目中,並將此代碼段調整為您的代碼:

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

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

Console.WriteLine(version);

使用System.Reflection.Assembly.GetEntryAssembly()。GetName()。版本以避免從dll獲取版本,將始終獲得當前“exe”的版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM