Can I automatically add file builds when using Visual Studio?

Posted by switchdoc on Sun, 01 Mar 2020 14:08:29 +0100

I just want to know how to use Visual Studio (2005) to automatically add builds (and versions?) to my files.

If I look for a property that says C:\Windows\notepad.exe, the Version tab will display "File version: 5.1.2600.2180.". I want to get these cool numbers in my dll Version, instead of Version 1.0.0.0, let's face it a little dull.

I tried something, but it didn't seem to be out of the box, or I was just looking for the wrong place (as usual).

I am mainly engaged in network projects

I looked at two:

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

I can't believe that trying to do something is standard practice.

Editor: as far as I know, it doesn't work in VS2005( http://www.codeproject.com/KB/dotnet/AutoIncrementVersion.aspx )

#1 building

Add the Delta (DateTime) information to the AssemblyFileVersion property, which has the advantage of not breaking any dependencies.

Boog based solution (doesn't work for me, maybe because of VS2008?) , you can use a combination of pre generated event generation files, add the file (including its version properties) and then use a method to read those values again. That's..

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 generated VersionInfo.cs file (Properties subfolder) in the project

Code to get the date (year to second):

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... And I don't know if it's going to produce a lot of forced rebuild (because files are always changing).

For example, if you update the VersionInfo.cs file every few minutes / hour (by using a temporary file and then copying / overwriting the real VersionInfo.cs when a sufficiently large change is detected), you can make it more intelligent. I have done it with great success.

#2 building

Perhaps, for this task, you can use the following code:

    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 invoke it from the form loading.
With this code, you can update any part of the file information in AssemblyInfo.cs (but you must use the standard directory structure).

#3 building

Another option to change the version number in each version is to use the MSBuild.Community.Tasks Version task for. Just download the installer, install it, then adjust the following code and paste it into the <Import Project= "$(MSBuildBinPath) \Microsoft.CSharp.targets" / > in the.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: adjust the StartDate property to your locale. It does not currently use unchanging culture.

For the third build on January 14, 2010, this will create a VersionInfo.cs with the following:

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

The file must then be added to the project (by adding an existing item), and the AssemblyVersion and AssemblyFileVersion lines must be removed from AssemblyInfo.cs.

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

#4 building

The change AssemblyInfo is valid in VS2012. It seems strange that there is no more support for this in Visual Studio, which you think is an essential part of the build / release process.

#5 building

AssemblyInfoUtil . Freedom. Open source.

Topics: Windows network