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
运行效果图