C# 如何在 .NET 应用程序中获取当前发布的版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1248824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the current published version in a .NET application?
提问by Ed Haber
I want to be able to display the current version of a .NET application that I have deployed using the publish wizard. There is a nice option to automatically update the version number every time I publish my application.
我希望能够显示我使用发布向导部署的 .NET 应用程序的当前版本。每次发布应用程序时,都有一个很好的选项可以自动更新版本号。
I found another question (Automatically update version number) that had this to get the current version:
我发现了另一个问题(自动更新版本号),它可以获取当前版本:
Assembly.GetExecutingAssembly().GetName().Version
This gets you the version you set in the project properties, but not the version that is automatically incremented each time you publish.
这将为您提供您在项目属性中设置的版本,而不是每次发布时自动递增的版本。
采纳答案by Ed Haber
I ended up using this little bit of code to get the current deployed version or if it isn't deployed the current assembly version.
我最终使用这一点代码来获取当前部署的版本,或者如果没有部署当前的程序集版本。
private Version GetRunningVersion()
{
try
{
return Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
}
catch
{
return Assembly.GetExecutingAssembly().GetName().Version;
}
}
I had to add references to System.Deployment
and System.Reflection
.
我不得不添加对System.Deployment
和的引用System.Reflection
。
回答by Jason
You can use the following test
您可以使用以下测试
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed) {
return System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
}
to avoid the exception (as detailed in this post).
以避免异常(如本文所述)。
Also, I don't think you can get the current publish version via Visual Studio debugging because accessing CurrentDeployment
will throw an InvalidDeploymentException
.
另外,我认为您无法通过 Visual Studio 调试获得当前发布版本,因为访问CurrentDeployment
会抛出InvalidDeploymentException
.
回答by Mark Micallef
Imports System.Configuration
Public Function GetAppVersion() As String
Dim ass As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim ver As System.Version = ass.GetName().Version
Return ver.Major & "." & ver.Minor & "." & ver.Revision
End Function
回答by Jhollman
Based in the answer from Jason, I ended up with this:
根据Jason的回答,我最终得到了这个:
Add Reference to System.Deployment.
添加对 System.Deployment 的引用。
string versionDeploy = Application.ProductVersion;
if (System.Diagnostics.Debugger.IsAttached)
{
this.lblVersion.Caption = string.Format("Versión {0} DESA", versionDeploy);
}
else
{
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
{
Version Deploy = System.Deployment.Application.ApplicationDeployment.CurrentDeployment.CurrentVersion;
versionDeploy = string.Format("{0}.{1}.{2}.{3}", Deploy.Major, Deploy.Minor, Deploy.Build, Deploy.Revision);
}
this.lblVersion.Caption = string.Format("Versión {0} PROD", versionDeploy);
}
Hope it helps.
希望能帮助到你。
回答by Manpreet Singh Dhillon
I used following solution for this problem, and it is working for me:
我对这个问题使用了以下解决方案,它对我有用:
DataSet ds = new DataSet();
ds.ReadXml(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MyProd.application"));
DataTable dt = new DataTable();
if (ds.Tables.Count > 1) {
dt = ds.Tables[1];
MessageBox.Show(dt.Rows[0]["version"].ToString());
}
回答by George
using System.Deployment.Application;
and
和
string yourPublishedVersionNumber=ApplicationDeployment.CurrentDeployment.CurrentVersion.ToString()