Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to: Specify Build Events (C#, J#) 

Use build events to specify commands that run before the build starts or after the build finishes. Build events are executed only if the build successfully reaches those points in the build process.

When a project is built, pre-build events are placed in a file called PreBuildEvent.bat and post-build events are placed in a file called PostBuildEvent.bat. If you want to ensure error checking, add your own error checking commands to the build steps.

NoteNote

The options available in dialog boxes, and the names and locations of menu commands you see, might differ from what is described in Help depending on your active settings or edition. This Help page was written with the General Development settings in mind. To view or change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

How to Specify Pre-Build and Post-Build Events

To specify a build event

  1. In Solution Explorer, select the project for which you want to specify the build event.

  2. On the Project menu, click Properties.

  3. Select the Build Events tab.

  4. In the Pre-build event command line box, specify the syntax of the build event.

    NoteNote

    Pre-build events do not run if the project is up to date and no build is triggered.

  5. In the Post-build event command line box, specify the syntax of the build event.

    NoteNote

    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.

  6. In the Run the post-build event box, specify under what conditions to run the post-build event.

    NoteNote

    To add lengthy syntax, or to select any build macros from the Pre-build Event/Post-build Event Command Line Dialog Box, click the Ellipsis button () to display an edit box.

    The build event syntax can include any command that is valid at the command line or in a .bat file. The name of a batch file should be preceded by call to ensure that all subsequent commands are executed.

Example: How to Change Manifest Information Using a Post-Build Event

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 4.10.0.0. To do this, the command will alter 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

  1. Create a console application for the command. From the File menu, click New, then Project.

  2. In the New Project dialog box, in the Visual C# node, select Windows, then the Console Application template. Name the project ChangeOSVersionCS.

  3. In Program.cs, add the following line to the other using statements at the top of the file:

    using System.Xml;
    
  4. In the ChangeOSVersionCS namespace, replace the Program class implementation with the following code:

    class Program
    {
       /// <summary>
       /// This function will set the minimum OS version for a ClickOnce application.
       /// </summary>
       /// <param name="args">
       /// Command Line Arguments:
       /// 0 - Path to application manifest (.exe.manifest).
       /// 1 - Version of OS
       ///</param>
       static void Main(string[] args)
       {
          string applicationManifestPath = args[0];
          Console.WriteLine("Application Manifest Path: " + applicationManifestPath);
    
          //Get version name
          Version osVersion = null;
          if (args.Length >=2 ){
             osVersion = new Version(args[1]);
          }else{
             throw new ArgumentException("OS Version not specified.");
          }
          Console.WriteLine("Desired OS Version: " + osVersion.ToString());
    
          XmlDocument document;
          XmlNamespaceManager namespaceManager;
          namespaceManager = new XmlNamespaceManager(new NameTable());
          namespaceManager.AddNamespace("asmv1", "urn:schemas-microsoft-com:asm.v1");
          namespaceManager.AddNamespace("asmv2", "urn:schemas-microsoft-com:asm.v2");
    
          document = new XmlDocument();
          document.Load(applicationManifestPath);
    
          string baseXPath;
          baseXPath = "/asmv1:assembly/asmv2:dependency/asmv2:dependentOS/asmv2:osVersionInfo/asmv2:os";
    
          //Change minimum required OS Version.
          XmlNode node;
          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);
       }
    }
    

    The command takes two arguments: the path to the application manifest (that is, the folder in which the build process creates the manifest, typically Projectname.publish), and the new operating system version.

  5. Build the project. On the Build menu, click Build Solution.

  6. Copy the .exe file to a directory such as C:\TEMP\ChangeOSVersionVB.exe.

Next, invoke this command in a post-build event to alter the application manifest.

To invoke a post-build event to alter the application manifest

  1. Create a Windows application for the project to be published. From the File menu, click New, then Project.

  2. In the New Project dialog box, in the Visual C# node, select Windows, then the Windows Application template. Name the project CSWinApp.

  3. With the project selected in Solution Explorer, on the Project menu, click Properties.

  4. In the Project Designer, go to the Publish page and set Publishing location to C:\TEMP\.

  5. Publish the project by clicking Publish Now.

    The manifest file will be built and placed in C:\TEMP\CSWinApp_1_0_0_0\CSWinApp.exe.manifest. To view the manifest, right-click on the file and click Open with, then Select the program from a list, then select Notepad.

    Search in the file for the <osVersionInfo> element. For example, the version might be:

    <os majorVersion="4" minorVersion="10" buildNumber="0" servicePackMajor="0" />
    
  6. In the Project Designer, go to the Build Events tab and click the Edit Post-build button.

  7. In the Post-build Event Command Line box, enter the following command:

    C:\TEMP\ChangeOSVersionCS.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, so $(TargetPath).manifest will specify the application manifest created in the bin directory. Publishing will copy this manifest to the publishing location that you set above.

  8. 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 on the file and click Open with, then Select the program from a list, then select Notepad.

    The version should now read:

    <os majorVersion="5" minorVersion="1" buildNumber="2600" servicePackMajor="0" />
    

See Also

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft