Can I automatically increment the file build version when using Visual Studio?

后端 未结 25 2516
-上瘾入骨i
-上瘾入骨i 2020-11-22 14:32

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

If I look up the properties of sa

相关标签:
25条回答
  • 2020-11-22 15:14

    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.

    0 讨论(0)
  • 2020-11-22 15:15

    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.

    0 讨论(0)
  • 2020-11-22 15:15

    To get incrementing (DateTime) information into the AssemblyFileVersion property which has the advantage of not breaking any dependencies.


    Building on Boog's solution (did not work for me, maybe because of VS2008?), you can use a combination of a pre-build event generating a file, adding that file (including its version properties) and then using a way to read out those values again. That is..

    Pre-Build-Event:

    echo [assembly:System.Reflection.AssemblyFileVersion("%date:~-4,4%.%date:~-7,2%%date:~-10,2%.%time:~0,2%%time:~3,2%.%time:~-5,2%")] > $(ProjectDir)Properties\VersionInfo.cs
    

    Include the resulting VersionInfo.cs file (Properties subfolder) into your project

    Code to get Date back (years down to seconds):

    var version = assembly.GetName().Version;
    var fileVersionString = System.Diagnostics.FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion;
    Version fileVersion = new Version(fileVersionString);
    var buildDateTime = new DateTime(fileVersion.Major, fileVersion.Minor/100, fileVersion.Minor%100, fileVersion.Build/100, fileVersion.Build%100, fileVersion.Revision);
    

    Not very comfortable.. also, I do not know if it creates a lot of force-rebuilds (since a file always changes).

    You could make it smarter for example if you only update the VersionInfo.cs file every few minutes/hours (by using a temporary file and then copying/overwriting the real VersionInfo.cs if a change large enough is detected). I did this once pretty successfully.

    0 讨论(0)
  • 2020-11-22 15:16

    I came up with a solution similar to Christians but without depending on the Community MSBuild tasks, this is not an option for me as I do not want to install these tasks for all of our developers.

    I am generating code and compiling to an Assembly and want to auto-increment version numbers. However, I can not use the VS 6.0.* AssemblyVersion trick as it auto-increments build numbers each day and breaks compatibility with Assemblies that use an older build number. Instead, I want to have a hard-coded AssemblyVersion but an auto-incrementing AssemblyFileVersion. I've accomplished this by specifying AssemblyVersion in the AssemblyInfo.cs and generating a VersionInfo.cs in MSBuild like this,

      <PropertyGroup>
        <Year>$([System.DateTime]::Now.ToString("yy"))</Year>
        <Month>$([System.DateTime]::Now.ToString("MM"))</Month>
        <Date>$([System.DateTime]::Now.ToString("dd"))</Date>
        <Time>$([System.DateTime]::Now.ToString("HHmm"))</Time>
        <AssemblyFileVersionAttribute>[assembly:System.Reflection.AssemblyFileVersion("$(Year).$(Month).$(Date).$(Time)")]</AssemblyFileVersionAttribute>
      </PropertyGroup>
      <Target Name="BeforeBuild">
        <WriteLinesToFile File="Properties\VersionInfo.cs" Lines="$(AssemblyFileVersionAttribute)" Overwrite="true">
        </WriteLinesToFile>
      </Target>
    

    This will generate a VersionInfo.cs file with an Assembly attribute for AssemblyFileVersion where the version follows the schema of YY.MM.DD.TTTT with the build date. You must include this file in your project and build with it.

    0 讨论(0)
  • 2020-11-22 15:18

    Maybe, for this task, you can use code like this:

        private bool IncreaseFileVersionBuild()
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                try
                {
                    var fi = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.GetDirectories("Properties")[0].GetFiles("AssemblyInfo.cs")[0];
                    var ve = System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    string ol = ve.FileMajorPart.ToString() + "." + ve.FileMinorPart.ToString() + "." + ve.FileBuildPart.ToString() + "." + ve.FilePrivatePart.ToString();
                    string ne = ve.FileMajorPart.ToString() + "." + ve.FileMinorPart.ToString() + "." + (ve.FileBuildPart + 1).ToString() + "." + ve.FilePrivatePart.ToString();
                    System.IO.File.WriteAllText(fi.FullName, System.IO.File.ReadAllText(fi.FullName).Replace("[assembly: AssemblyFileVersion(\"" + ol + "\")]", "[assembly: AssemblyFileVersion(\"" + ne + "\")]"));
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            return false;
        }
    

    and call it from form loading.
    With this code you can update any part of file info in AssemblyInfo.cs (but you must use "standard" directory structure).

    0 讨论(0)
  • 2020-11-22 15:19

    Install the Build Version Increment add-in. It gives you way more control than the * option.

    0 讨论(0)
提交回复
热议问题