. NET's automatic serial number tool

Source: Internet
Author: User
Tags log

Operating environment: Visual Studio. NET


Introduced
Each of us from the original development environment to the user under the Vs.net, have encountered a lot of obstacles. One of the obstacles I have encountered is that my original macro cannot continue to work.

Now the compile serial number automatic growth tool is a combination of many people's code snippets, although it is not perfect, and lack of some features, but at least it provides a solid foundation for further development.

Goal
Here is the need to compile the serial number automatically:

1. At the time of compiling each release, automatically increase the compiled serial number saved in a separate file, which will be added to my project.

2. Automatically record the date and serial number of the compilation.



Code Analysis
My first task was to find a replacement for vs 6. Application_beforebuildstart () event, VS. NET designers provide us with a very easy to use compilation events:

Onbuildbegin
OnBuildDone
Onbuildprojconfigbegin
OnBuildProjConfigDone
The naming conventions for these event handlers are a bit misleading. We don't want to use onbuildbegin, because even if we compile for multiple configurations (release, Debug, and so on), It will only be executed once. This makes it difficult to increase the compilation sequence number only when compiling release versions. Onbuildprojconfigbegin can be executed when compiling for each configuration. It also provides a string parameter named Projectconfig to describe which configuration is being used by the current compilation.

Most of the tasks are done in the Onbuildprojconfigbegin event handler, which also uses two auxiliary macro:

Writetologfile
Writetooutputbuildpane
Both macros can be written to the Onbuildprojconfigbegin event handler or deleted when not in use.



Code
' ------------------------------------
' Onbuildprojconfigbegin event handler
' ------------------------------------

Private Sub Buildevents_onbuildprojconfigbegin (
ByVal Project as String,
ByVal Projectconfig as String,
ByVal Platform as String,
ByVal Solutionconfig as String)
Handles Buildevents.onbuildprojconfigbegin

' Abort if build type is debug

If InStr (1, Projectconfig, "Debug", 1) Then Exit Sub

' Get ver filename

Dim Res_filename as String

Res_filename = DTE. Solution.fullname
Res_filename = Path.changeextension (Res_filename, ". Ver")

' Open VERSION FILE and increment build number

Dim Msg_text as String

If file.exists (res_filename) Then

Dim Line as String

Try

Dim sr as StreamReader = New StreamReader (res_filename)
line = Sr. ReadLine ()
Sr. Close ()

Catch ex as Exception

Module1.writetooutputbuildpane (VbCrLf & _
"Version file read failed:" &
Ex. Message & VbCrLf)

End Try

line = right (line, line. LENGTH-18)

Try

Dim SW As StreamWriter = File.createtext (res_filename)
Sw. WriteLine ("#define Build_num {0}", line + 1)
Sw. Close ()

Catch ex as Exception

Module1.writetooutputbuildpane (VbCrLf & _
"Version File Write failed:" &
Ex. Message & VbCrLf)

End Try

Msg_text = ' Build number: ' & Line + 1 & ', ' & Now

Module1.writetooutputbuildpane (VbCrLf & Msg_text & VbCrLf)
Module1.writetologfile (Msg_text)

Else

Try

Dim SW As StreamWriter = File.createtext (res_filename)
Sw. WriteLine ("#define BUILD_NUM 1")
Sw. Close ()

Catch ex as Exception

Module1.writetooutputbuildpane (VbCrLf & _
"Version File Write failed:" &
Ex. Message & VbCrLf)

End Try

Msg_text = "Build Number:1," & Now

Module1.writetooutputbuildpane (VbCrLf & Msg_text & VbCrLf)
Module1.writetologfile (Msg_text)

End If

End Sub




' ----------------------------------
' Write text message to a log file
' ----------------------------------

Sub Writetologfile (ByVal msg_text as String)

Dim Log_filename as String

Log_filename = DTE. Solution.fullname
Log_filename = Path.changeextension (Log_filename, ". Log.txt")

Try

Dim SW As StreamWriter = File.appendtext (log_filename)
Sw. WriteLine (Msg_text)
Sw. Close ()

Catch ex as Exception

MsgBox ("Log file Write failed:" & ex. Message)

End Try

End Sub




' ----------------------------------------------------------------
' Write a text message to the ' build pane of the Output tool window
' ----------------------------------------------------------------

Sub Writetooutputbuildpane (ByVal msg_text as String)

' Create a tool window handle for the Output window.

Dim win as Window = DTE. Windows.item (EnvDTE.Constants. _
Qvswindowkindoutput)

' Create handles to the ' Output window and it build pane.

Dim OW as OutputWindow = win. Object
Dim OWP as OutputWindowPane

OWP = OW. OutputWindowPanes.Item ("Build")

' Add a line of ' the ' the ' Output pane.

Owp.outputstring (Msg_text & VbCrLf)

End Sub
Integration
Attention! If you don't have a macro project, create one first. You need to open a macro project and display it in the VS Macros IDE. (Note: You can use the shortcut key ALT + F8, or click View-> other Windows-> Macro Explorer to open the Macro Navigation window.) Double-click one of the macros to edit.








1. Each macro project has a page called EnvironmentEvents, double-click to open it.

2. From the class name Drop-down list, select BuildEvents.

3. Select Onbuildprojconfigbegin from the method name Drop-down list.

4. Add two reference declarations (import).
The declaration of an exception class needs to reference the System namespace. The action class for the file affirms the need to reference System.IO.

5. Add in the Onbuildprojconfigbegin method.





6. Double hit Open Module1 page.

Module1 is a default page name used by the IDE. If you use a different name, be sure to modify the corresponding namespace in the Onbuildprojconfigbegin.

Add two secondary methods.
Add pairs. Reference to System and. Systemio.



Run
Save the editing results, and then compile to check for errors. Once the compilation is successful, it can be used directly. It is simple to use, and this code is executed at every compile time.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.