[英]Getting the Version of my C# app?
I am working on desktop application.我正在开发桌面应用程序。 I have create a setup.我已经创建了一个设置。
Ex.前任。 My Application.我的应用程序。 Version is 1.0.0.版本为 1.0.0。
I want to get the current version of my desktop application which is 1.0.0
.我想获取我的桌面应用程序的当前版本,即1.0.0
。 I have tried by using Application.ProductVersion
but it provides the version of my controls.我尝试过使用Application.ProductVersion
但它提供了我的控件的版本。 (I am using DevExpress Control
15.2.7, so it provides the current version as 15.2.7
). (我使用的是DevExpress Control
15.2.7,所以它提供当前版本为15.2.7
)。
How can I get the current version of the installed application?如何获取已安装应用程序的当前版本? I want to compare it to the installed version to provide a "New Version Available" functionality for my product.我想将它与已安装的版本进行比较,以便为我的产品提供“新版本可用”功能。
The info you are looking for is in AssemblyInfo.cs .您要查找的信息位于AssemblyInfo.cs 中。
To access the info written in there at runtime you can use the System.Reflection.Assembly
.要在运行时访问其中写入的信息,您可以使用System.Reflection.Assembly
。
Use System.Reflection.Assembly.GetExecutingAssembly()
to get the assembly ( that this line of code is in ) or use System.Reflection.Assembly.GetEntryAssembly()
to get the assembly your project started with ( most likely this is your app ).使用System.Reflection.Assembly.GetExecutingAssembly()
获取程序集(这行代码所在)或使用System.Reflection.Assembly.GetEntryAssembly()
获取您的项目开始使用的程序集(很可能这是您的应用程序) .
In multi-project solutions this is something to keep in mind!在多项目解决方案中,这是需要牢记的!
string version = Assembly.GetExecutingAssembly().GetName().Version.ToString()
// returns 1.0.0.0
Corresponding AssemblyInfo.cs :对应的AssemblyInfo.cs :
Corresponding EXE-properties :对应的EXE 属性:
This may be important when working with InstallShield (see comments) !这在使用 InstallShield 时可能很重要(见评论)!
System.Reflection.Assembly executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var fieVersionInfo = FileVersionInfo.GetVersionInfo(executingAssembly .Location);
var version = fieVersionInfo.FileVersion;
Get the version of a specific assembly:获取特定程序集的版本:
private const string AssemblyName = "MyAssembly"; // Name of your assembly
public Version GetVersion()
{
// Get all the assemblies currently loaded in the application domain.
Assembly[] assemblies = Thread.GetDomain().GetAssemblies();
for (int i = 0; i < assemblies.Length; i++)
{
if (string.Compare(assemblies[i].GetName().Name, AssemblyName) == 0)
{
return assemblies[i].GetName().Version;
}
}
return Assembly.GetExecutingAssembly().GetName().Version; // return current version assembly or return null;
}
Another approach, which is basically the same as the accepted answer, is:另一种与公认答案基本相同的方法是:
Version appVersion = Assembly.GetExecutingAssembly().GetName().Version;
versionLabel.Text = "v" + appVersion.Major + "." + appVersion.Minor + "." + appVersion.Build + ".";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.