Imports System Imports EnvDTE Imports EnvDTE80 Imports System.Diagnostics ' 此模块在vs2005上调试通过 Public Module ModuleVersionAutoIncrease 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 Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As Boolean = True) As OutputWindowPane Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput) If show Then win.Visible = True Dim ow As OutputWindow = win.Object Dim owpane As OutputWindowPane Try owpane = ow.OutputWindowPanes.Item(Name) Catch e As System.Exception owpane = ow.OutputWindowPanes.Add(Name) End Try owpane.Activate() Return owpane End Function Sub Application_BuildFinish(ByVal numError, ByVal numWarning) Dim full_path full_path = GetProjectDir(DTE.Solution.Projects.Item(1).FullName) full_path = full_path & "versionno.h" '得到版本文件名 Dim fileOperate As IO.File Dim fileOperateWrite As IO.File Dim bFileExists = fileOperate.Exists(full_path) Dim sStreamReader As IO.StreamReader Dim sStreamWirte As IO.FileStream Dim iMarjorVersion As Integer Dim iMinorVersion As Integer Dim iModifyTimes As Integer Dim iBuildTimes As Integer iMarjorVersion = 1 iMinorVersion = 0 iModifyTimes = 0 iBuildTimes = 0 If bFileExists = True Then Dim sFileContent As String sStreamReader = fileOperate.OpenText(full_path) sFileContent = sStreamReader.ReadLine() sFileContent = sFileContent.Replace("#define", "") sFileContent = sFileContent.Replace("FILEVER", "") sFileContent = sFileContent.Replace(" ", "") Dim sSplitContent() As String sSplitContent = sFileContent.Split(",") If sSplitContent.GetLength(0) <> 4 Then sStreamReader.Close() GoTo Label_Rebuild End If iMarjorVersion = CInt(sSplitContent(0)) iMinorVersion = CInt(sSplitContent(1)) iModifyTimes = CInt(sSplitContent(2)) iBuildTimes = CInt(sSplitContent(3)) sStreamReader.Close() End If iBuildTimes = iBuildTimes + 1 Label_Rebuild: Dim sfileVerLine As String Dim sProductVerLine As String Dim sStrefileVerLine As String Dim sStrProductVerLine As String Dim sDataTime As String Dim sBeWrite As String Dim iLen As Integer Dim sOutputDate As String Dim sOutput As String Dim oData As Date sfileVerLine = "#define FILEVER " + CStr(iMarjorVersion) + "," + CStr(iMinorVersion) + "," + CStr(iModifyTimes) + "," + CStr(iBuildTimes) + vbCrLf sProductVerLine = "#define PRODUCTVER " + CStr(iMarjorVersion) + "," + CStr(iMinorVersion) + "," + CStr(iModifyTimes) + "," + CStr(iBuildTimes) + vbCrLf sStrefileVerLine = "#define STRFILEVER " & Chr(34) & CStr(iMarjorVersion) & ", " & CStr(iMinorVersion) & ", " & CStr(iModifyTimes) & ", " & CStr(iBuildTimes) & "/0" & Chr(34) & vbCrLf sStrProductVerLine = "#define STRPRODUCTVER " & Chr(34) & CStr(iMarjorVersion) & ", " & CStr(iMinorVersion) & ", " & CStr(iModifyTimes) & ", " & CStr(iBuildTimes) & "/0" & Chr(34) & vbCrLf sBeWrite = sfileVerLine + sProductVerLine + sStrefileVerLine + sStrProductVerLine oData = System.DateTime.Now sOutputDate = "// Last Modified On Time :" & DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") & vbCrLf sBeWrite = sBeWrite & sOutputDate iLen = sBeWrite.Length Dim iLenIndex As Integer Dim sByteTmp As Byte Dim sAyBtye(iLen) As Byte Dim sTringTmp As String For iLenIndex = 0 To iLen - 1 Step 1 sTringTmp = sBeWrite.Substring(iLenIndex, 1) sByteTmp = Convert.ToByte(Asc(sTringTmp)) sAyBtye.SetValue(sByteTmp, iLenIndex) Next sStreamWirte = fileOperate.Open(full_path, IO.FileMode.Create) sStreamWirte.Write(sAyBtye, 0, iLen) sStreamWirte.Close() Label_WriteMsg: Dim win As Window = DTE.Windows.Item(Constants.vsWindowKindCommandWindow) Dim targetWnd As Object If (DTE.ActiveWindow Is win) Then targetWnd = win.Object Else targetWnd = GetOutputWindowPane("生成") End If sOutput = "版本已经从 " + CStr(iMarjorVersion) + "," + CStr(iMinorVersion) + "," + CStr(iModifyTimes) + "," + CStr(iBuildTimes - 1) + " 升到了 " _ + CStr(iMarjorVersion) + "," + CStr(iMinorVersion) + "," + CStr(iModifyTimes) + "," + CStr(iBuildTimes) targetWnd.OutputString(sOutput.ToString()) End Sub End Module