Getting the Version of my C# app?
Solution 1
The info you are looking for is in AssemblyInfo.cs.
To access the info written in there at runtime you can use the 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).
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:
Corresponding EXE-properties:
This may be important when working with InstallShield (see comments) !
Solution 2
System.Reflection.Assembly executingAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var fieVersionInfo = FileVersionInfo.GetVersionInfo(executingAssembly .Location);
var version = fieVersionInfo.FileVersion;
Solution 3
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;
}
Solution 4
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 + ".";
Nanji Mange
Updated on July 09, 2022Comments
-
Nanji Mange almost 2 years
I am working on desktop application. I have create a setup.
Ex. My Application. Version is 1.0.0.
I want to get the current version of my desktop application which is
1.0.0
. I have tried by usingApplication.ProductVersion
but it provides the version of my controls. (I am usingDevExpress Control
15.2.7, so it provides the current version as15.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.
-
Leri about 8 yearsDepends how your application is implemented. Basically, question as it stands, can't be answered.
-
Nanji Mange about 8 years@Leri It is setup created by InstallShield Express Edition 2015.
-
-
Nanji Mange about 8 yearsIt returns "15.2.7.0" which is my DevExpress Component version.
-
Nitin about 8 yearswhat is executingAssembly .Location giving you? path of your application exe or something else?
-
Nanji Mange about 8 yearsHere is all parameter. (1) Assembly Location: C:\Program Files (x86)\MyApplication\MyApplication.exe. (2) File Version/Product Version: 15.2.7.0
-
Felix D. about 8 yearsPlease check your AssemblyInfo.cs. What is written in there ?
-
Felix D. about 8 yearsif this doesn't help anything try working with
GetEntryAssembly
rather thenGetExecutingAssembly
-
Nanji Mange about 8 yearsI didn't set Assembly Info. I am working 3rd party InstallShield for creating setup. I have set version and all in it but didn't set in Assembly. I thought InstallShield will manage. Anyways, I have changed Assembly info. It is working fine now. Let me know if I still understand wrong.
-
Sinatr about 8 years"I thought InstallShield will manage" - that's wrong assumption, see this.
-
Clonkex about 2 yearsI was originally using
Assembly.GetEntryAssembly()
which worked while debugging under Visual Studio and while running standalone, but when the XAML designer loaded it would return the current Visual Studio version (or the version of some component). This solution works in all three situations :D