0

I have a simple console application and it has auto increment build version. When I build project, I’d like to append build version to end of the output exe file name with using post-build event. There is no macro for build version.

Batuhan Avlayan
  • 411
  • 1
  • 4
  • 19

1 Answers1

0

You will need a small utility to rename the target:

using System;
using System.Diagnostics;
using System.IO;

namespace RenameWithVersion
{
  public class Program
  {
    public static void Main(String[] args)
    {
      if (args.Length != 1)
        throw new Exception("There should only be one command line parameter - the file to be renamed.");

      var oldFilename = Path.GetFullPath(args[0]);

      if (!File.Exists(oldFilename))
        throw new Exception($"File '{oldFilename}' does not exist.");

      var oldFilenameWithoutExt = Path.ChangeExtension(oldFilename, null);
      var fileVersion = FileVersionInfo.GetVersionInfo(oldFilename).FileVersion;
      var ext = Path.GetExtension(oldFilename);

      var newFilename = $"{oldFilenameWithoutExt}{fileVersion}{ext}";

      File.Delete(newFilename);
      File.Move(oldFilename, newFilename);
    }
  }
}

Compile it and put the executable somewhere on your path.

In the post-build event, call the utility like this:

RenameWithVersion "$(TargetPath)"

(Remember to put $(TargetPath) in double quotes in case the path contains spaces.)

Chris R. Timmons
  • 2,187
  • 1
  • 13
  • 11