visual-studio - VS.net 在一个解决方案中为多个项目设置版本

标签 visual-studio version assemblyinfo

我正在尝试更新解决方案中的许多不同项目以获得新版本号。有没有一种简单的方法可以在所有项目的文件版本和 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

最佳答案

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

然后我link the file to each project :

  • 项目上的上下文菜单并选择“添加现有项目”
  • 从根文件夹中选择文件
  • 单击“添加”按钮旁边的下拉菜单并选择“添加为链接”

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

    相反,我使用 nant 来编译解决方案并使用 asminfo生成 AssemblyVersion.cs 文件的任务。

    关于visual-studio - VS.net 在一个解决方案中为多个项目设置版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1393683/

    相关文章:

    wpf - 绑定(bind)字符串格式数字逗号且无小数位

    c++ - 错误 C2679: "binary ' <<': no operator found which takes a right-hand operand of type ' std::string'(或没有可接受的转换)”

    ios - NSFoundationVersionNumber 和 iOS 版本

    vba - 使用 VBA 查找 MS Office 修订版和构建版本

    linux - Debian 操作系统内核版本差异 : 3. 16.51-3 vs 3.16.51-3+deb8u1

    visual-studio-2010 - 构建我的应用程序时警告 MSB3283 : Cannot find wrapper assembly for type library "MSComctlLib".

    visual-studio - Visual Studio 2013 在发布云服务时卡住

    C# 项目全局 AssemblyInfo

    c# - 帮助 C# 中的程序集信息文件

    c# - 在 win 7 上的 visual studio 2012 中找不到 assemblyInfo.cd 的构建错误