簡體   English   中英

如何使用post build事件將構建版本附加到控制台應用程序中的輸出exe文件名的末尾?

[英]How to append build version to end of the output exe file name in console appilication with using post build event?

我有一個簡單的控制台應用程序,它有自動增量構建版本。 當我構建項目時,我想使用post-build事件將構建版本附加到輸出exe文件名的末尾。 構建版本沒有宏。

您將需要一個小實用程序來重命名目標:

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);
    }
  }
}

編譯它並將可執行文件放在路徑上的某個位置。

在post-build事件中,像這樣調用實用程序:

RenameWithVersion“$(TargetPath)”

(記住將$(TargetPath)放在雙引號中,以防路徑包含空格。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM