Automatically update version number

I would like the version property of my application to be incremented for each build but I'm not sure on how to enable this functionality in Visual Studio (2005/2008). I have tried to specify the AssemblyVersion as 1.0.* but it doesn't get me exactly what I want.

I'm also using a settings file and in earlier attempts when the assembly version changed my settings got reset to the default since the application looked for the settings file in another directory.

I would like to be able to display a version number in the form of 1.1.38 so when a user finds a problem I can log the version they are using as well as tell them to upgrade if they have an old release.

A short explanation of how the versioning works would also be appreciated. When does the build and revision number get incremented?


With the "Built in" stuff, you can't, as using 1.0.* or 1.0.0.* will replace the revision and build numbers with a coded date/timestamp, which is usually also a good way.

For more info, see the Assembly Linker Documentation in the /v tag.

As for automatically incrementing numbers, use the AssemblyInfo Task:

AssemblyInfo Task

This can be configured to automatically increment the build number.

There are 2 Gotchas:

  • Each of the 4 numbers in the Version string is limited to 65535. This is a Windows Limitation and unlikely to get fixed.
  • Why are build numbers limited to 65535?
  • Using with with Subversion requires a small change:
  • Using MSBuild to generate assembly version info at build time (including SubVersion fix)
  • Retrieving the Version number is then quite easy:

    Version v = Assembly.GetExecutingAssembly().GetName().Version;
    string About = string.Format(CultureInfo.InvariantCulture, @"YourApp Version {0}.{1}.{2} (r{3})", v.Major, v.Minor, v.Build, v.Revision);
    

    And, to clarify: In .net or at least in C#, the build is actually the THIRD number, not the fourth one as some people (for example Delphi Developers who are used to Major.Minor.Release.Build) might expect.

    In .net, it's Major.Minor.Build.Revision.


    VS.NET defaults the Assembly version to 1.0.* and uses the following logic when auto-incrementing: it sets the build part to the number of days since January 1st, 2000, and sets the revision part to the number of seconds since midnight, local time, divided by two. See this MSDN article.

    Assembly version is located in an assemblyinfo.vb or assemblyinfo.cs file. From the file:

    ' Version information for an assembly consists of the following four values:
    '
    '      Major Version
    '      Minor Version 
    '      Build Number
    '      Revision
    '
    ' 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")> 
    

    I have found that it works well to simply display the date of the last build using the following wherever a product version is needed:

    System.IO.File.GetLastWriteTime(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString("yyyy.MM.dd.HH.mm.ss")
    

    Rather than attempting to get the version from something like the following:

    System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
    object[] attributes = assembly.GetCustomAttributes(typeof(System.Reflection.AssemblyFileVersionAttribute), false);
    object attribute = null;
    
    if (attributes.Length > 0)
    {
        attribute = attributes[0] as System.Reflection.AssemblyFileVersionAttribute;
    }
    
    链接地址: http://www.djcxy.com/p/86110.html

    上一篇: 在.NET中打破变化

    下一篇: 自动更新版本号