代码之家  ›  专栏  ›  技术社区  ›  CodingBarfield

一个解决方案中多个项目的vs.net集版本

  •  4
  • CodingBarfield  · 技术社区  · 16 年前

    我试图在一个解决方案中更新许多不同的项目,以获得新的版本号。是否有一种简单的方法来同步所有项目的版本号文件版本和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
    
    1 回复  |  直到 12 年前
        1
  •  5
  •   bryanbcook    12 年前

    我通常将程序集版本属性存储在单独的 程序集版本.cs 归档并将其放在我的解决方案的根文件夹中。

    然后我 link 每个项目的文件:

    1. 项目上的上下文菜单并选择“添加现有项”
    2. 从根文件夹中选择文件
    3. 单击“添加”按钮旁边的下拉菜单,然后选择“添加为链接”

    不幸的是,在msbuild中,我没有找到一种在解决方案编译之前自动生成版本号的干净方法。(我相信msbuild只针对每个项目,而不是针对每个解决方案-- 也许外面有人知道 更新: 看见 here for solution-wide pre-build events through msbuild )

    相反,我使用nant编译解决方案并使用 asminfo 要生成的任务 程序集版本.cs 文件。