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

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

提交回复
热议问题