views:

9639

answers:

12

I was just wondering how I could automatically increment build (and version?) of my files when using Visual Studio (2005).

If I look up the properties of say C:\Windows\notepad.exe, the Version tab gives "File version: 5.1.2600.2180". I would like to get these cool numbers in the version of my dll's too, not version 1.0.0.0, which let's face it is a bit dull.

I tried a few things, but it doesn't seem to be out-of-box functionality, or maybe I'm just looking in the wrong place (as usual)

I work with mainly web projects....

Yah, I looked at both

  1. http://www.codeproject.com/KB/dotnet/Auto_Increment_Version.aspx
  2. http://www.codeproject.com/KB/dotnet/build_versioning.aspx

and I couldn't believe it so much effort to do something is standard practise.

EDIT: It does not work in VS2005 as far I can tell (http://www.codeproject.com/KB/dotnet/AutoIncrementVersion.aspx)

A: 

Each time I do a build it auto-increments the least-significant digit.

I don't have any idea how to update the others, but you should at least be seeing that already...

Brian Knoblauch
+3  A: 

Go to Project | Properties and then Assembly Information and then Assembly Version and put an * in the last or the second-to-last box (you can't auto-increment the Major or Minor components).

MusiGenesis
+3  A: 

To get the version numbers try

 System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly()
 System.Reflection.AssemblyName assemblyName = assembly.GetName();
 Version version = assemblyName.Version;

To set the version number, create/edit AssemblyInfo.cs

 [assembly: AssemblyVersion("1.0.*")]
 [assembly: AssemblyFileVersion("1.0.*")]

Also as a side note, the third number is the number of days since 2/1/2000 and the fourth number is half of the amount of total seconds in the day. So if you compile at midnight it should be zero.

Bob
+30  A: 

open up the AssemblyInfo.cs file and change

// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

to

[assembly: AssemblyVersion("1.0.*")]
//[assembly: AssemblyFileVersion("1.0.0.0")]

you can do this in IDE by going to project -> properties -> assembly information

This however will only allow you to auto increment the Assembly version and will give you the "Assembly File Version: A wildcard ("*") is not allowed in this feild" message box if you try place a * in the file version field.

So just open up the assemblyinfo.cs and do it manually.

Hath
nice and simple just what i was after
DrG
Yes I just encountered the ""Assembly File Version: A wildcard ("*") is not allowed in this field" that's what won your method the green tick :D
DrG
Does this work in VS2008?
Greg B
Does not work in VS2005. Installing SP1 to see what happens
DrG
Nope, not VS2005 SP1
DrG
@greg - yes it should work in vs 2008
Hath
@in.spite - thats odd i have VS2005 pro and it seems to work the same as vs 2008.
Hath
this works: [assembly: AssemblyVersion("1.0.*")] //[assembly: AssemblyFileVersion("1.0.0.0")]
DrG
You need to do it in the AssemblyInfo.cs file as discussed. Unfortunately the "Assembly Information" dialog throws an error for our "benefit". Discussion here: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=252521
Topdown
+3  A: 

Set the version number to "1.0.*" and it will automatically fill in the last two number with the date (in days from some point) and the time (half the seconds from midnight)

James Curran
hey if I had read this properly at the beginning I would have saved myself mucho agro. thx
DrG
A: 

It is in your project properties under publish

http://screencast.com/t/Vj7rhqJO

Alex
+5  A: 

Setting a * in the version number in AssemblyInfo or under project properties as described in the other posts does not work with all versions of Visual Studio / .NET.

Afaik it did not work in VS 2005 (but in VS 2003 and VS 2008). For VS 2005 you could use the following: Auto Increment Visual Studio 2005 version build and revision number on compile time.

But be aware that changing the version number automatically is not recommended for strong-named assemblies. The reason is that all references to such an assembly must be updated each time the referenced assembly is rebuilt due to the fact that strong-named assembly references are always a reference to a specific assembly version. Microsoft themselves change the version number of the .NET Framework assemblies only if there are changes in interfaces. (NB: I'm still searching for the link in MSDN where I read that.)

0xA3
I think for any version of VS you can only put the * in the Build or Revision boxes. I just tried this out using VS 2005, and it works fine. I'm not sure what the author of that code project article is talking about.
MusiGenesis
Maybe it came back with a service pack, but I remember that it did not use to work when I was using VS 2005.
0xA3
It does not work with 2005, I'll look for a service pack and report back.
DrG
Maybe MusiGenesis has an add-on installed which is enabling automatic versioning.
0xA3
@divo: no, I'm add-on-phobic. I just have Visual Studio 2005 Professional SP1. I've never seen a problem with the *, but I usually increment manually. Sounds like a weird bug.
MusiGenesis
+13  A: 

In visual Studio 2008, the following works.

Find the AssemblyInfo.cs file and find these 2 lines:

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

You could try changing this to:

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.*")]

But this won't give you the desired result, you will end up with a Product Version of 1.0.* and a File Version of 1.0.0.0. Not what you want!

However, if you remove the second of these lines and just have:

[assembly: AssemblyVersion("1.0.*")]

Then the compiler will set the File Version to be equal to the Product Version and you will get your desired result of an automatically increment product and file version which are in sync. E.g. 1.0.3266.92689

Sam Meldrum
This works as good as anything else, and works in VS2005. I was hoping for some rational number like 1.0.1.56 in lieu I get 1.0.3266.30135 but at least it increases (albeit by some random number :D)
DrG
oh i just read it : it will automatically fill in the last two number with the date (in days from some point) and the time (half the seconds from midnight)
DrG
+1  A: 

As of right now, for my application,

string ver = Application.ProductVersion;

returns

ver = 1.0.3251.27860

The value 3251 is the number of days since 1/1/2000. I use it to put a version creation date on the splash screen of my application. When dealing with a user, I can ask the creation date which is easier to communicate than some long number.

(I'm a one-man dept supporting a small company. This approach may not work for you.)

SeaDrive
+2  A: 

Use the AssemblyInfo task from the MSBuild Community Tasks (http://msbuildtasks.tigris.org/) project, and integrate it into your .csproj/.vbproj file.

It has a number of options, including one to tie the version number to the date and time of day.

Recommended.

devstuff
+4  A: 

Just install the Build Version Increment addin. This little addin gives you way more control then the * option.

+3  A: 

Another option for changing version numbers in each build is to use the Version task of MSBuild.Community.Tasks. Just download their installer, install it, then adapt the following code and paste it after <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> in your .csproj file:

<Import Project="$(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.Targets" />
<Target Name="BeforeBuild">
    <Version VersionFile="Properties\version.txt" Major="1" Minor="0" BuildType="Automatic" StartDate="12/31/2009" RevisionType="BuildIncrement">
      <Output TaskParameter="Major" PropertyName="Major" />
      <Output TaskParameter="Minor" PropertyName="Minor" />
      <Output TaskParameter="Build" PropertyName="Build" />
      <Output TaskParameter="Revision" PropertyName="Revision" />
    </Version>
    <AssemblyInfo CodeLanguage="CS"
                  OutputFile="Properties\VersionInfo.cs"
                  AssemblyVersion="$(Major).$(Minor)"
                  AssemblyFileVersion="$(Major).$(Minor).$(Build).$(Revision)" />
</Target>

Note: Adapt the StartDate property to your locale. It currently does not use the invariant culture.

For the third build on January 14th, 2010, this creates a VersionInfo.cs with this content:

[assembly: AssemblyVersion("1.0")]
[assembly: AssemblyFileVersion("1.0.14.2")]

This file then has to be added to the project (via Add existing item), and the AssemblyVersion and AssemblyFileVersion lines have to be removed from AssemblyInfo.cs.

The different algorithms for changing the version components are described in $(MSBuildExtensionsPath)\MSBuildCommunityTasks\MSBuild.Community.Tasks.chm and Version Properties.

Christian