1

我创建了一个 C# 项目,它采用单个参数(作为受影响项目的 AssemblyInfo 文件的位置),然后将 AssemblyFileVersion 的修订号更新 1。该程序从 Properties --> Build Events --> Post 调用-build 事件命令行使用:

调用 "C:\Users\SomeUser\Documents\Visual Studio 2013\Projects\VersionUpdate\VersionUpdate\bin\Release\VersionUpdate.exe" "$(ProjectDir)Properties\AssemblyInfo.cs"

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace VersionUpdate
{
class Program
{
    static void Main( string[] args )
    {
        string assemblyInfoFile = args[ 0 ]; //Read In From Command Line Argument 1
        string line; //Holds the line of the Original AssemblyInfo.cs file being read
        string currentAssemblyFileVersion = ""; //Holds the current AssemblyFileVersion
        string newAssemblyFileVersion = ""; //Holds the new AssemblyFileVersion
        string revision = ""; //Holds the revision portion of AssemblyFileVersion ( until end of line )
        string start = ""; //Holds the AssemblyFileVersion line up to and including the last .
        string tail = ""; //Holds the tail of the AssemblyFileVersion after the Revision Number
        int periods = 0; //Holds the number of . that have been counted
        int end = 0; //Holds the index of the final " after the Revision Number
        int revisionNumber = 0; //Holds the integer representation of the Revision Number
        int newRevisionNumber = 0; //Holds the incremented Revision Number

        Regex exp = new Regex( ".*\\bassembly: AssemblyFileVersion\\b.*" ); //Matches the proper line

        //Establish new FileStream for Reading
        FileStream fs = new FileStream( assemblyInfoFile, FileMode.Open, FileAccess.Read, FileShare.None );

        //Create StreamReader to Read AssemblyInfo file
        StreamReader sr = new StreamReader( fs );

        //Loop through AssemblyInfo file searching for a Match
        while ( ( line = sr.ReadLine() ) != null )
        {
            Match test = exp.Match( line );

            //If we found a match, save the matched line as the current AssemblyFileVersion
            if ( test.Success )
            {
                currentAssemblyFileVersion = line;
            }
        }

        //Search the current AssemblyFileVersion to locate the proper position and retrieve the Revision Number
        for ( int i = 0; i < currentAssemblyFileVersion.Length; i++ )
        {
            char piece = currentAssemblyFileVersion[ i ];

            //Look for . and increment periods when found
            if ( piece == '.' )
            {
                periods++;
            }

            //Save the Revision Number once the final . is found
            if ( periods == 3 )
            {
                //Reset periods so that looping does not overwrite results
                periods = 0;

                start = currentAssemblyFileVersion.Substring( 0, i + 1 ); //Store the beginning of the AssemblyFileVersion string, including the final .
                revision = currentAssemblyFileVersion.Substring( i + 1 ); //Store the reaminder of the AssemblyFileVersion string
                end = revision.IndexOf( '\"' ); //Locate the index of the final " after the Revision Number
                tail = revision.Substring( end ); //Store the remaining string after the Revision Number
                revisionNumber = Convert.ToInt16( revision.Substring( 0, end ) ); //Convert the Revision Number to an int
            }
        }

        newRevisionNumber = ++revisionNumber; //Preincrement and store Revision Number
        newAssemblyFileVersion = start + newRevisionNumber + tail; //Create new AssemblyFileVersion string

        //Close the FileStream
        fs.Close();

        //Read the entire AssemblyInfo file and replace the affected line
        string wholeFile = File.ReadAllText( assemblyInfoFile );
        wholeFile = wholeFile.Replace( currentAssemblyFileVersion, newAssemblyFileVersion );
        File.WriteAllText( assemblyInfoFile, wholeFile );
    }
}
}

然后我抓住了 AssemblyFileVersion:

using System.Diagnostics;
using System.Reflection;

Assembly assembly = Assembly.GetExecutingAssembly();
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo( assembly.Location );
string version = versionInfo.FileVersion;

我发布此内容是为了帮助处于类似情况的其他人,因为很难找到相关信息,而且,我绝不是专家,我想知道是否有更好的方法可以完成此任务。

4

0 回答 0