How to: Specify Build Events (Visual Basic)
Updated: June 2011
Build events in Visual Basic can be used to run scripts, macros, or other actions as a part of the compilation process. Pre-build events occur before compilation; post-build events occur after compilation.
Build events are specified in the Build Events dialog box, available from the Compile page of the Project Designer.
You cannot specify build events from the Visual Basic Express IDE.
To specify a build event
With a project selected in Solution Explorer, on the Project menu, click Properties.
Click the Compile tab.
Click the Build Events button to open the Build Events dialog box.
Enter the command-line arguments for your pre-build or post-build action, and then click OK.
Add a call statement before all post-build commands that run .bat files. For example, call C:\MyFile.bat or call C:\MyFile.bat call C:\MyFile2.bat.
If your pre-build or post-build event does not complete successfully, you can terminate the build by having your event action exit with a code other than zero (0), which indicates a successful action.
The following procedure shows how to set the minimum operating system version in the application manifest using an .exe command called from a post-build event (the .exe.manifest file in the project directory). The minimum operating system version is a four-part number such as 220.127.116.11. To do this, the command will change the <dependentOS> section of the manifest:
<dependentOS> <osVersionInfo> <os majorVersion="4" minorVersion="10" buildNumber="0" servicePackMajor="0" /> </osVersionInfo> </dependentOS>
To create an .exe command to change the application manifest
Create a console application for the command. From the File menu, click New, and then click Project.
In the New Project dialog box, in the Visual Basic node, select Windows and then the Console Application template. Name the project ChangeOSVersionVB.
In Module1.vb, add the following line to the other Imports statements at the top of the file:
Add the following code in Sub Main:
Sub Main() Dim applicationManifestPath As String applicationManifestPath = My.Application.CommandLineArgs(0) Console.WriteLine("Application Manifest Path: " & applicationManifestPath.ToString) 'Get version name Dim osVersion As Version If My.Application.CommandLineArgs.Count >= 2 Then osVersion = New Version(My.Application.CommandLineArgs(1).ToString) Else Throw New ArgumentException("OS Version not specified.") End If Console.WriteLine("Desired OS Version: " & osVersion.ToString()) Dim document As XmlDocument Dim namespaceManager As XmlNamespaceManager namespaceManager = New XmlNamespaceManager(New NameTable()) With namespaceManager .AddNamespace("asmv1", "urn:schemas-microsoft-com:asm.v1") .AddNamespace("asmv2", "urn:schemas-microsoft-com:asm.v2") End With document = New XmlDocument() document.Load(applicationManifestPath) Dim baseXPath As String baseXPath = "/asmv1:assembly/asmv2:dependency/asmv2:dependentOS/asmv2:osVersionInfo/asmv2:os" 'Change minimum required OS Version. Dim node As XmlNode node = document.SelectSingleNode(baseXPath, namespaceManager) node.Attributes("majorVersion").Value = osVersion.Major.ToString() node.Attributes("minorVersion").Value = osVersion.Minor.ToString() node.Attributes("buildNumber").Value = osVersion.Build.ToString() node.Attributes("servicePackMajor").Value = osVersion.Revision.ToString() document.Save(applicationManifestPath) End Sub
The command takes two arguments. The first argument is the path to the application manifest (that is, the folder in which the build process creates the manifest, typically Projectname.publish). The second argument is the new operating system version.
On the Build menu, click Build Solution.
Copy the .exe file to a directory such as C:\TEMP\ChangeOSVersionVB.exe.
Next, invoke this command in a post-build event to change the application manifest.
To invoke a post-build event to change the application manifest
Create a Windows application for the project to be published. From the File menu, click New, and then click Project.
In the New Project dialog box, in the Visual Basic node, select Windows and then the Windows Application template. Name the project VBWinApp.
With the project selected in Solution Explorer, on the Project menu, click Properties.
In the Project Designer, go to the Publish page and set Publishing location to C:\TEMP\.
Publish the project by clicking Publish Now.
The manifest file will be built and put in C:\TEMP\VBWinApp_1_0_0_0\VBWinApp.exe.manifest. To view the manifest, right-click the file and click Open with, then click Select the program from a list, and then click Notepad.
Search in the file for the <osVersionInfo> element. For example, the version might be:
<os majorVersion="4" minorVersion="10" buildNumber="0" servicePackMajor="0" />
In the Project Designer, go to the Compile tab and click the Build Events button to open the Build Events dialog box.
In the Post-build Event Command Line box, enter the following command:
C:\TEMP\ChangeOSVersionVB.exe "$(TargetPath).manifest" 5.1.2600.0
When you build the project, this command will change the minimum operating system version in the application manifest to 5.1.2600.0.
The $(TargetPath) macro expresses the full path for the executable being created. Therefore, $(TargetPath).manifest will specify the application manifest created in the bin directory. Publishing will copy this manifest to the publishing location that you set earlier.
Publish the project again. Go to the Publish page and click Publish Now.
View the manifest again. To view the manifest, go to the publish directory, right-click the file and click Open with and then Select the program from a list, and then click Notepad.
The version should now read:
<os majorVersion="5" minorVersion="1" buildNumber="2600" servicePackMajor="0" />