.NET程序编译版本维护

 

1、  工具-宏-新建宏项目

2、  将Model1.vb重命名为AutoBuildVersion.vb

 

添加此事件到原生成文件中

 

    Private Sub BuildEvents_OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildBegin

        Call AutoBuildVersion()

    End Sub


然后拷贝如下代码到AutoBuildVersion.vb中

Imports System
Imports EnvDTE
Imports System.Diagnostics

Public Module BuildVersion

    '得到项目所在目录   
    Function GetProjectDir(ByVal FullName)
        Dim proj_path
        proj_path = Split(StrReverse(FullName), "\", -1, 1)
        Dim count
        count = UBound(proj_path)
        Dim full_path
        full_path = ""
        Dim i
        For i = 1 To count
            full_path = full_path & "\" & proj_path(i)
        Next
        GetProjectDir = StrReverse(full_path)
    End Function
    ' This event will be triggered after every build of a project   
    ' You can modify the code below to only update projects that are active  
    ' It currently will scan all projects in the solution for AssemblyInfo.vb files  
    ' to update.    
    Sub AutoBuildVersion()

        'Comment the follow 3 lines, if you want the build number to increment even if the build fails    
        If DTE.Solution.SolutionBuild.LastBuildInfo() <> 0 Then
            Exit Sub
        End If

        ' Change this, if you would only like to modify the AssemblyInfo file in active project files       
        ' For Each proj As Project In DTE.ActiveSolutionProjects     
        For Each proj As Project In DTE.Solution.Projects

            Dim full_path

            full_path = GetProjectDir(proj.FullName)

            ' Attempt to open the AssemblyInfo.vb file        
            full_path = full_path & "Properties\AssemblyInfo.cs"
            Try
                DTE.ItemOperations.OpenFile(full_path).Activate()

                Dim activeDoc As Document = DTE.ActiveDocument
                Dim objSelection As TextSelection

                objSelection = DTE.ActiveDocument.Selection

                Dim objStartPosition As Integer

                ' Get the text between AssemblyVersion(" and ")     
                objSelection.FindText("AssemblyFileVersion(""")
                objStartPosition = objSelection.BottomPoint.DisplayColumn

                Dim objEndPosition As Integer

                objSelection.FindText(""")")
                objEndPosition = objSelection.AnchorPoint.DisplayColumn

                ' Get back to after the AssemblyVersion" part           
                objSelection.StartOfLine()
                objSelection.FindText("AssemblyFileVersion(""")
                objSelection.Collapse()
                objSelection.MoveToDisplayColumn(0, objEndPosition, True)
                objSelection.Text = Now.ToString("yyyy.MM.dd.HHmm")
                activeDoc.Close(vsSaveChanges.vsSaveChangesYes)

            Catch

            End Try
        Next
    End Sub



End Module


 运行效果图

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值