4

ソリューション内のさまざまなプロジェクトを更新して、新しいバージョン番号を取得しようとしています。すべてのプロジェクトの fileversion と clickonce オプションでバージョン番号を同期する簡単な方法はありますか?

答え

最後に、小さなツールを作成して問題を解決しました。

    Sub Main()
    Try
        Console.WriteLine("Updating version numbers")

        Dim strPath As String = System.AppDomain.CurrentDomain.BaseDirectory()
        Dim strAppName As String = ""
        Console.WriteLine(strPath)
        If My.Application.CommandLineArgs.Count > 0 Then
            Console.WriteLine(My.Application.CommandLineArgs(0))
            strPath = My.Application.CommandLineArgs(0)
            strAppName = My.Application.CommandLineArgs(1)
        Else
            strPath = "C:\Projects\APP\"
            Console.WriteLine("Error loading settings")
        End If


        Dim strAssemblyInfoFile As String = strPath + "Properties\AssemblyInfo.cs"
        If Not File.Exists(strAssemblyInfoFile) Then
            strAssemblyInfoFile = strPath + "My Project\AssemblyInfo.vb"
        End If
        Console.WriteLine("Loading " + strAssemblyInfoFile)

        Dim strFileContent As String
        strFileContent = ReadFileText(strAssemblyInfoFile)

        Dim AssemblyVersionRegex As New Regex("AssemblyVersion(?:Attribute)?\(\s*?""(?<version>(?<major>[0-9]+)\.(?<minor>[0-9]+)\.(?<build>[0-9]+)\.(?<revision>[0-9]+))""\s*?\)")

        Dim strOldVersion As String = AssemblyVersionRegex.Match(strFileContent).Groups("version").Value
        Dim oldVersion As New Version(strOldVersion)

        Dim newVersion As New Version(oldVersion.Major.ToString + "." + oldVersion.Minor.ToString + "." + oldVersion.MajorRevision.ToString + "." + (oldVersion.MinorRevision + 1).ToString)
        Dim strNewVersion As String = newVersion.ToString()

        Console.WriteLine("Newversion " + strNewVersion)

        'Replace oldversion to newversion
        strFileContent = strFileContent.Replace(strOldVersion, strNewVersion)

        File.WriteAllText(strAssemblyInfoFile, strFileContent)

        Dim strProjectFile As String = strPath + strAppName + ".csproj"
        If Not File.Exists(strProjectFile) Then
            strProjectFile = strPath + strAppName + ".vbproj"
        End If

        Console.WriteLine("Loading " + strProjectFile)

        strFileContent = File.ReadAllText(strProjectFile)

        strFileContent = strFileContent.Replace(strOldVersion, strNewVersion)

        Dim strOld As String = "<ApplicationRevision>" + oldVersion.MinorRevision.ToString() + "</ApplicationRevision>"
        Dim strNew As String = "<ApplicationRevision>" + (oldVersion.MinorRevision + 1).ToString() + "</ApplicationRevision>"

        strFileContent = strFileContent.Replace(strOld, strNew)

        SaveFile(strProjectFile, strFileContent)

        Console.WriteLine("Done")

    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try
End Sub

Function ReadFileText(ByVal strFilePath As String) As String
    Return File.ReadAllText(strFilePath)
End Function

Sub SaveFile(ByVal strFilePath As String, ByVal strData As String)
    File.WriteAllText(strFilePath, strData)
End Sub
4

1 に答える 1

5

通常、アセンブリ バージョンの属性を別のAssemblyVersion.csファイルに格納し、ソリューションのルート フォルダーに配置します。

次に、ファイルを各プロジェクトにリンクします。

  1. プロジェクトのコンテキストメニューを開き、「既存のアイテムを追加」を選択します
  2. ルートフォルダからファイルを選択
  3. [追加] ボタンの横にあるドロップダウン メニューをクリックし、[リンクとして追加] を選択します。

残念ながら、MSBuild でソリューションをコンパイルする前にバージョン番号を自動生成するクリーンな方法を見つけられませんでした。(MSBuild には、ソリューションごとではなく、プロジェクトごとにのみイベントがあると思います。他の誰かが 更新を知っている可能性があります。 msbuild によるソリューション全体のビルド前イベントについては、こちらを参照してください)

代わりに、nant を使用してソリューションをコンパイルし、asminfoタスクを使用してAssemblyVersion.csファイルを生成します。

于 2009-09-08T18:32:38.090 に答える