Versioning of programs is very important in software development. I control the version of the scheme as follows:
The version number generally follows the form: AA.BB.CC.DDDD. Where AA,BB,CC is defined by itself, dddd is automatically incremented by the version control macro I do. A AA represents a large version number, with large updates and an increase in functionality when changing this number. After this version is upgraded, BB, CC clear zero. BB represents the minor level version number, with minor updates and features added when changing this number. After this version is upgraded, CC clear zero. CC represents a smaller level version number, which is used to represent a bug correction. Change this number each time a bug is fixed. DD represents the number of compilations, which are incremented sequentially, without zeroing.
The macro response I wrote Onbuildbegin automatically increases the version number of dddd each time you compile project. This macro currently supports c#,vc++, should also support VB, but did not test, hehe. There are a few things to explain: For C # project, modify only the AssemblyVersion. The FileVersion segment in AssembleInfo.cs was deleted. For VC project, the around version number in the. rc that was modified. Because of the visual stdio features of the language (guess this is the reason), for VC Project, after modifying the. rc file, you must use Unicode encoding output to enable support for multiple languages, such as Chinese. For C # project, it is not required and cannot be output in Unicode encoding.
The code is as follows:
In EnvironmentEvents, respond to Buildbegin events
Private Sub Buildevents_onbuildbegin (ByVal Scope as Envdte.vsbuildscope, ByVal Action as envdte.vsbuildaction) Handles Bu Ildevents.onbuildbegin Start () End Sub
The main is as follows:
Imports System Imports EnvDTE Imports EnvDTE80 Imports System.Diagnostics Imports System.IO Imports System.Text.RegularEx Pressions public Module Main ' path to file AssemblyInfo.cs, relative to Path of the project ReadOnly M_sassemblyinfofilep Ath as String = "Properties/assemblyinfo.cs" Dim M_outputwindowpane as OutputWindowPane ' Regular expression to match the Version string ReadOnly m_sversion As String = "assemblyversion/(& Chr) &" ([0-9]+)/. ( [0-9]+]/. ([0-9]+)/. ([0-9/.] +) "& Chr &"/) "ReadOnly m_sfileversion as String =" FileVersion ([0-9]+)/, ([0-9]+)/, ([0-9]+)/, ([0-9/.] +) "ReadOnly m_sproductversion as String =" productversion ([0-9]+)/, ([0-9]+)/, ([0-9]+)/, ([0-9/.] +) "ReadOnly m_sfileversion2 as String =" VALUE "+ Chr (+) +" FileVersion "+ Chr (+) +", "+ Chr (34) +" ([0-9]+)/. ([0-9]+)/. ([0-9]+)/. ([0-9/.] +) "+ Chr (ReadOnly) m_sproductversion2 as String =" VALUE "+ Chr +" productversion "+ Chr (+) +", "+ Chr (34) +" ([0-9]+)/. ([0-9]+)/. ([0-9]+)/.([0-9/.] +) "+ Chr Function getoutputwindowpane (ByVal sname as String, Optional ByVal Show as Boolean = True) as OUTPUTWINDOWPA Ne Dim win as Window = DTE. Windows.item (EnvDTE.Constants.vsWindowKindOutput) If show Then win. Visible = True Dim OutputWindow as OutputWindow = win. Object Dim OutputWindowPane as OutputWindowPane Try OutputWindowPane = OutputWindow.OutputWindowPanes.Item (sname) Catch e as System.Exception outputwindowpane = OUTPUTWINDOW.OUTPUTWINDOWPANES.ADD (sname) end Try If show Then OutputWindowPane. Activate () return OutputWindowPane end Function Sub Start () Dim project as Project M_outputwindowpane = Getoutputwindowpan E ("Build", False) project = CType (DTE. ActiveSolutionProjects (). GetValue (0), envdte.project) If Project. ConfigurationManager.ActiveConfiguration.ConfigurationName = "Release" Then m_outputwindowpane.outputstring ("[ Dsverctrl]: Release builds, skip version control. "& Microsoft.VisualBasic.Constants.vbCrLf" Exit Sub-End If pro Ject. Codemodel.language= Codemodellanguageconstants.vscmlanguagecsharp Or project. Codemodel.language = Codemodellanguageconstants.vscmlanguagevb Then ' m_outputwindowpane.outputstring ("[DSVerCtrl]: The project is not C # project, pass the version of Control. & Microsoft.VisualBasic.Constants.vbCrLf) Updatecsharpversio N (Project) Exit Sub ElseIf project. Codemodel.language = Codemodellanguageconstants.vscmlanguagevc Then ' m_outputwindowpane.outputstring ("[DSVerCtrl]: The project is VC project, pass the version control. & Microsoft.VisualBasic.Constants.vbCrLf) Updatevcversion (Proje CT) Exit Sub Else m_outputwindowpane.outputstring ([Dsverctrl]: The project is not C #, VB or VC project, pass the version Control. ' & Microsoft.VisualBasic.Constants.vbCrLf ' end If End Sub Private Sub Updatecsharpversion (ByVal Project as Pr Oject Dim VSProject As Vslangproj.vsproject Dim Sprojectpath As String Dim sassemblyinfofile As String Dim scontent as St Ring Dim Verstrregex As Regex Dim match As Match Dim nver as IntegeR Dim snewver as String vsproject = CType (project. Object (), vslangproj.vsproject) Sprojectpath = VsProject.Project.Properties.Item ("FullPath"). Value sassemblyinfofile = sprojectpath & M_sassemblyinfofilepath ' outputwindowpane.outputstring (' [DSVerCtrl]: Changing the version of "" + Project. FullName + "" "..." + Microsoft.VisualBasic.Constants.vbCrLf) ' M_outputwindowpane.outputstring ("[Dsverctrl]: changing The version of "" "+ Sprojectpath +" "..." + Microsoft.VisualBasic.Constants.vbCrLf) m_outputwindowpane.outputstring ("[ Dsverctrl]: Modifying the version of "" "& Project. Name & "" ... "& Microsoft.VisualBasic.Constants.vbCrLf) ' M_outputwindowpane.outputstring (" [Dsverctrl]: Assembly Info file name is "" "+ Sassemblyinfofile +" "". "+ Microsoft.VisualBasic.Constants.vbCrLf) scontent = Readfileco Ntent (Sassemblyinfofile) ' M_outputwindowpane.outputstring ("[Dsverctrl]: File content is" "" + Scontent + "" + MICROSOFT.V isualBasic.Constants.vbCrLf) Verstrregex = New Regex (m_sversion) match = Verstrregex.match (scontent) If match. Success Then ' m_outputwindowpane.outputstring ("[Dsverctrl]: Found match string." + Microsoft.VisualBasic.Constants.vbCrLf) Nver = Convert.ToInt32 (match. Groups.item (4). Value) Nver = nver + 1 scontent = Scontent.remove (match. Groups.item (4). Index, match. Groups.item (4). Length) scontent = Scontent.insert (match. Groups.item (4). Index, Nver.tostring ()) Else m_outputwindowpane.outputstring ("[Dsverctrl]: Can not find the version string." & Micros Oft. VISUALBASIC.CONSTANTS.VBCRLF) End If snewver = match. Groups.item (1). Value & "." & Match. Groups.item (2). Value & "." & Match. Groups.item (3). Value & "." & Nver.tostring () m_outputwindowpane.outputstring ("[Dsverctrl]: Modified the version to" "" & Snew Ver & "" ". & Microsoft.VisualBasic.Constants.vbCrLf) ' M_outputwindowpane.outputstring (" [Dsverctrl]: File Content after modify is "" + Scontent + "" + Microsoft.VisualBasic.Constants.vbCrLf) writefilecontent (SASSEMBLYinfofile, Scontent, False) End Sub Private Sub updatevcversion (ByVal project as Project) Dim VSProject as Vslangproj.vspr Oject Dim Sprojectpath As String Dim sassemblyinfofile As String Dim scontent As String Dim verstrregex As Regex Dim match As Match Dim nver As Integer Dim snewver As String VSProject = CType (project. Object (), vslangproj.vsproject) Sprojectpath = VsProject.Project.Properties.Item ("Projectdirectory"). Value ' m_outputwindowpane.outputstring ("[Dsverctrl]: Changing the version of" "" + Sprojectpath + "" "..." + microsoft.visu alBasic.Constants.vbCrLf) Sassemblyinfofile = Sprojectpath + project. Name + ". RC" ' Outputwindowpane.outputstring ("[Dsverctrl]: Changing the version of" "" + Project. FullName + "" "..." + Microsoft.VisualBasic.Constants.vbCrLf) m_outputwindowpane.outputstring ("[Dsverctrl]: modifying The version of "" "& Project. Name & "" ... "& Microsoft.VisualBasic.Constants.vbCrLf) ' M_outputwindowpane.outputstring (" [Dsverctrl]: Recourse file name is "" "+Sassemblyinfofile + "" "." + Microsoft.VisualBasic.Constants.vbCrLf) scontent = readfilecontent (sassemblyinfofile) ' m_ Outputwindowpane.outputstring ("[Dsverctrl]: File content is" "" + Scontent + "" + Microsoft.VisualBasic.Constants.vbCrLf Verstrregex = New Regex (m_sfileversion) match = Verstrregex.match (scontent) If match. Success Then ' m_outputwindowpane.outputstring ("[Dsverctrl]: Found match string." + Microsoft.VisualBasic.Constants.vbCrLf) Nver = Convert.ToInt32 (match. Groups.item (4). Value) Nver = nver + 1 scontent = Scontent.remove (match. Groups.item (4). Index, match. Groups.item (4). Length) scontent = Scontent.insert (match. Groups.item (4). Index, Nver.tostring ()) Else m_outputwindowpane.outputstring ("[Dsverctrl]: Can not find the FileVersion version string." & Microsoft.VisualBasic.Constants.vbCrLf) End If Verstrregex = New Regex (m_sproductversion) match = Verstrregex.match (scontent) If Match. Success Then ' m_outputwindowpane.outputstring ("[Dsverctrl]: Found match string." + Microsoft. VisualBasic.Constants.vbCrLf) scontent = Scontent.remove (match. Groups.item (4). Index, match. Groups.item (4). Length) scontent = Scontent.insert (match. Groups.item (4). Index, nver.tostring () Else m_outputwindowpane.outputstring ("[Dsverctrl]: Can not find" productversion version String. "& Microsoft.VisualBasic.Constants.vbCrLf" end If Verstrregex = New Regex (m_sfileversion2) match = Verstrrege X.match (scontent) If Match. Success Then ' m_outputwindowpane.outputstring ("[Dsverctrl]: Found match string." + Microsoft.VisualBasic.Constants.vbCrLf) scontent = Scontent.remove (match. Groups.item (4). Index, match. Groups.item (4). Length) scontent = Scontent.insert (match. Groups.item (4). Index, Nver.tostring ()) Else m_outputwindowpane.outputstring ("[Dsverctrl]: Can not find the FileVersion version string." & Microsoft.VisualBasic.Constants.vbCrLf) End If Verstrregex = New Regex (m_sproductversion2) match = Verstrregex.match (scontent) If Match. Success Then ' m_outputwindowpane.outputstring ("[DsverctrL]: Found match string. "+ Microsoft.VisualBasic.Constants.vbCrLf" scontent = Scontent.remove (match. Groups.item (4). Index, match. Groups.item (4). Length) scontent = Scontent.insert (match. Groups.item (4). Index, nver.tostring () Else m_outputwindowpane.outputstring ("[Dsverctrl]: Can not find" productversion version String. "& Microsoft.VisualBasic.Constants.vbCrLf" end If snewver = match. Groups.item (1). Value & "." & Match. Groups.item (2). Value & "." & Match. Groups.item (3). Value & "." & Nver.tostring () m_outputwindowpane.outputstring ("[Dsverctrl]: Modified the version to" "" & Snew Ver & "" ". & Microsoft.VisualBasic.Constants.vbCrLf) ' M_outputwindowpane.outputstring (" [Dsverctrl]: File Content after modify is "" + Scontent + "" + Microsoft.VisualBasic.Constants.vbCrLf) writefilecontent (Sassemblyinfofile, Scontent, True) end Sub ' Read file content to a string Private Function readfilecontent (ByVal sfilename As String) as St Ring Dim Readerstream as StrEamreader Dim sfilecontent as String Try readerstream = New StreamReader (sfilename, True) Catch e as FileNotFoundException M_outputwindowpane.outputstring ("[Dsverctrl]-Error:can not find" "" & sFileName & "" "." & Microso Ft. VisualBasic.Constants.vbCrLf) Exit Function Catch e as Exception m_outputwindowpane.outputstring ("[Dsverctrl]-ERROR: Open file "" "& sFileName &" "Failed:" & E.message & Microsoft.VisualBasic.Constants.vbCrLf) Exit funct Ion End Try sfilecontent = Readerstream.readtoend () readerstream.close () return sfilecontent end Function ' Write ' Modi fied file content back Private Sub writefilecontent (ByVal sfilename As String, ByVal scontent as String, ByVal Bvcproject As Boolean) Dim Writerstream as StreamWriter Try If (bvcproject) Then writerstream = New StreamWriter (sfilename, False, Sy Stem. Text.Encoding.Unicode) Else Writerstream = New StreamWriter (sfilename, False) end If Catch e as FileNotFoundException M_ou Tputwindowpane.outpUtstring ("[Dsverctrl]-Error:can not find" "" & sFileName & "" "." & Microsoft.VisualBasic.Constants . vbCrLf) Exit Sub Catch e as Exception m_outputwindowpane.outputstring ("[Dsverctrl]-error:write file" "& Sfilenam E & "" Failed: "& E.message & Microsoft.VisualBasic.Constants.vbCrLf) Exit Sub end Try Writerstream.write (SC ontent) Writerstream.flush () writerstream.close () End Sub-end Module