Determine assembly version during a post-build event

后端 未结 10 1560
故里飘歌
故里飘歌 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:43

    I think the best thing you can do is look at MSBuild and MsBuild Extension Pack you should be able to edit you solution file so that a post build event occurs and writes to your test file.

    If this is too complicated then you could simply create a small program that inspects all assemblies in you output directory and execute it on post build, you could pass in the output directory using the variable name... for example in the post build event...

    AssemblyInspector.exe "$(TargetPath)"

    class Program
    {
        static void Main(string[] args)
        {
            var assemblyFilename = args.FirstOrDefault();
            if(assemblyFilename != null && File.Exists(assemblyFilename))
            {
                try
                {
                    var assembly = Assembly.ReflectionOnlyLoadFrom(assemblyFilename);
                    var name = assembly.GetName();
    
                    using(var file = File.AppendText("C:\\AssemblyInfo.txt"))
                    {
                        file.WriteLine("{0} - {1}", name.FullName, name.Version);
                    }
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
    }
    

    You could also pass in the text file location...

提交回复
热议问题