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

发布于 2024-08-03 17:40:53 字数 3145 浏览 3 评论 0 原文

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

I am trying to update a lot of different projects in a solution to have a new version number. Is there a simple way to synchronize the version number across all projects fileversion and clickonce options?

Answer

Finally solved the problem by writing a little tool:

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

并安 2024-08-10 17:40:53

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

然后我链接将文件添加到每个项目

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

不幸的是,我还没有在 MSBuild 中找到一种干净的方法来在解决方案编译。(我相信 MSBuild 仅具有每个项目的事件,而不是每个解决方案 - 也许其他人知道 更新: 请参阅此处通过 msbuild 进行解决方案范围的预构建事件

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

I typically store the assembly version attributes in a separate AssemblyVersion.cs file and place it at the root folder of my solution.

Then I link the file to each project:

  1. Context-menu on Project and choose "Add Existing Item"
  2. Select the file from the root folder
  3. Click on the Drop-down menu next to the "Add" button" and select "Add as Link"

Unfortunately, I haven't found a clean way in MSBuild to auto-generate the version number before the solution compiles. (I believe MSBuild only has events per project, not per solution -- maybe someone else out there knows update: see here for solution-wide pre-build events through msbuild)

Instead, I use nant to compile the solution and use the asminfo task to generate the AssemblyVersion.cs file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文