views:

123

answers:

4

I would like to programmaticly get a stored major version number for a C# project in both Debug and Release builds.

Where could I store this version number and how do i access it?

+4  A: 

Go to Project Properties | Application | Assembly Information.

You can access the version via Assembly.GetExecutingAssembly().GetName().Version

Not Sure
+4  A: 

The version number is typically defined in the AssemblyInfo.cs file (located in the Properties folder). You can get the version number programmatically like this:

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

Or, if you want to access the different parts:

Version version = Assembly.GetExecutingAssembly().GetName().Version;
Console.WriteLine("Major={0}, Minor={1}", version.Major, version.Minor);

See the Version class documentation for more details.

Fredrik Mörk
beat me to it :(
Stan R.
...Version.ToString(x), where x is the number of parts of the version information, usually works better than referencing the individual properties. http://msdn.microsoft.com/en-us/library/bff8h2e1.aspx
Todd Ropog
@ToddR: That's a good point. The idea behind that sample in my answer was more to indicate that the Version object has those elements as properties, should you want to do something with them. You could for instance want to construct a string like "Version 1.3, build 3252", where you could use .ToString(2) for the first part, but would need to access the Build property separately for the build number.
Fredrik Mörk
@Fredrik Mörk: Absolutely. I didn't mean to suggest what you had done was bad. I was just adding to the information, because I know a lot of people overlook the overloaded ToString().
Todd Ropog
A: 

I guess it depends on what you mean by store it, but you should look at:

ApplicationDeployment.CurrentDeployment.CurrentVersion

Edit: Actually this is for getting your deployed version. It might not be what you need.

joshlrogers
+2  A: 

to get your assembly version programmatically.

Assembly.GetExecutingAssembly().GetName().Version

Here is how to get the file version

System.Diagnostics.FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location)
Stan R.