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

后端 未结 25 2513
-上瘾入骨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:08

    In Visual Studio 2019

    It was not enough for me adding

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

    When building it throws me this error

    The specified version string does not conform to the required format

    Solution

    The format was finally accepted after I set Deterministic to False in project.csproj

    <Deterministic>false</Deterministic>
    

    Edit:

    For some reason setting Deterministic to False messed up my config file loading it and saving it on different locations.

    Workaround:

    I setup a post-build event to increment the revision number:

    Post-Build Event batch script

    This calls a powershell script named autoincrement_version.ps1 passing as argument the path of AssemblyInfo.cs

    if $(ConfigurationName) == Release (
    PowerShell -ExecutionPolicy RemoteSigned $(ProjectDir)autoincrement_version.ps1 '$(ProjectDir)My Project\AssemblyInfo.cs'
    )
    

    Poweshell script

    It autoincrements the revision number using Regex

    param( [string]$file );
      $regex_revision = '(?<=Version\("(?:\d+\.)+)(\d+)(?="\))'
      $found = (Get-Content $file) | Select-String -Pattern $regex_revision
      $revision = $found.matches[0].value
      $new_revision = [int]$revision + 1
      (Get-Content $file) -replace $regex_revision, $new_revision | Set-Content $file -Encoding UTF8
    
    0 讨论(0)
  • 2020-11-22 15:08

    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...

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

    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

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

    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)

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

    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).

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

    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.

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