Determine assembly version during a post-build event

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

    From that what I understand...

    You need a generator for post build events.

    1. Step: Writing a Generator

    /*
    * Author: Amen RA
    * # Timestamp: 2013.01.24_02:08:03-UTC-ANKH
    * Licence: General Public License
    */
    using System;
    using System.IO;
    
    namespace AppCast
    {
        class Program
        {
            public static void Main(string[] args)
            {
                // We are using two parameters.
    
                // The first one is the path of a build exe, i.e.: C:\pathto\nin\release\myapp.exe
                string exePath = args[0];
    
                // The second one is for a file we are going to generate with that information
                string castPath = args[1];
    
                // Now we use the methods below
                WriteAppCastFile(castPath, VersionInfo(exePath));
            }
    
    
            public static string VersionInfo(string filePath)
            {
                System.Diagnostics.FileVersionInfo myFileVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(filePath);
                return myFileVersionInfo.FileVersion;
            }
    
    
            public static void WriteAppCastFile(string castPath, string exeVersion)
            {
                TextWriter tw = new StreamWriter(castPath);
                tw.WriteLine(@"");
                tw.WriteLine(@"");
                tw.WriteLine(@"MyApp - New version! Release " + exeVersion + " is available.");
                tw.WriteLine(@"" + exeVersion + "");
                tw.WriteLine(@"http://www.example.com/pathto/updates/MyApp.exe");
                tw.WriteLine(@"http://www.example.com/pathto/updates/MyApp_release_notes.html");
                tw.WriteLine(@"");
                tw.Close();
            }
        }
    }
    

    2. Step: Using it as a post build command in our IDE

    After the application is running satisfyingly for you:

    In your development IDE, use the following command line for post build events.

    C:\Projects\pathto\bin\Release\AppCast.exe "C:\Projects\pathto\bin\Release\MyApp.exe" "c:\pathto\www.example.com\root\pathto\updates\AppCast.xml"
    

提交回复
热议问题