Determine assembly version during a post-build event

后端 未结 10 1590
故里飘歌
故里飘歌 2020-11-27 11:58

Let\'s say I wanted to create a static text file which ships with each release. I want the file to be updated with the version number of the release (as specified in A

10条回答
  •  暖寄归人
    2020-11-27 12:32

    As a workaround I've written a managed console application which takes the target as a parameter, and returns the version number.

    I'm still interested to hear a simpler solution - but I'm posting this in case anyone else finds it useful.

    using System;
    using System.IO;
    using System.Diagnostics;
    using System.Reflection;
    
    namespace Version
    {
        class GetVersion
        {
            static void Main(string[] args)
            {
                if (args.Length == 0 || args.Length > 1) { ShowUsage(); return; }
    
                string target = args[0];
    
                string path = Path.IsPathRooted(target) 
                                    ? target 
                                    : Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + Path.DirectorySeparatorChar + target;
    
                Console.Write( Assembly.LoadFile(path).GetName().Version.ToString(2) );
            }
    
            static void ShowUsage()
            {
                Console.WriteLine("Usage: version.exe ");
            }
        }
    }
    

提交回复
热议问题