Build Events in Visual Studio

The Build Events in Visual Studio makes life much easier. I use them primarily to copy the files around after building the projects, but we can use them to run any command that we want. We have two types of build events.

  1. Pre-build event
  2. Post-build event

Let's have a look at the options.

Go to Solution Explorer and right-click on the project then select Properties then go to the Build Events tab.

click Build Events

If you click on the Edit Pre/Post build event command line button the following dialog will open.

build event command

But, when we click the "Macros", we can see how Visual Studio can really help us out:


The following I will explain how I use them.

First Scenario

I have a requirement to maintain three different configurations (web.config) files for Web.LocalDev.config, Web.QA.config and Web.Production.config. When I build the project based upon the Configuration name selection the main web.config file must be updated.

Assume we select the Production configuration name then the main Web.config file will be updated with Web.Production.config.

The following  is the procedure to do that.

  1. Create new configurations.

    Go to the Solution Configuration files dropdown and select the Configuration manager.

    Goto Solution Configuration

    The Configuration Manager dialog box will open and expand the Active solution configuration dropdown and click on New.

    Active solution configuration

    Once you clicked on New, the New Solution configuration dialog box will open, enter the configuration Name and click on the OK button.

    configuration dialog box

    Now you can see it in Solution configuration files list.


  2. Creating various configuration files.

    After creating the Configuration names in the Solution configuration files, go to the main Web.config file and right-click on it and select Add config transform then it will add the config file as shown in the following screen shot.

    add config transform

  3. Now writing scrpts for post-build events.

    Before that we need to create a batch file (with the name copyifnewer.bat) to compare the file names of the selected config name and config file name. The following shows how I specified the batch file code.
    1. @echo off  
    2. echo Comparing two files: %1 with %2  
    4. if not exist %1 goto File1NotFound  
    5. if not exist %2 goto File2NotFound  
    7. fc %1 %2   
    8. if %ERRORLEVEL%==0 GOTO NoCopy  
    10. echo Files are not the same. Copying %1 over %2  
    11. copy %1 %2 /y & goto END  
    13. :NoCopy  
    14. echo Files are the same. Did nothing  
    15. goto END  
    17. :File1NotFound  
    18. echo %1 not found.  
    19. goto END  
    21. :File2NotFound  
    22. copy %1 %2 /y  
    23. goto END  
    25. :END  
    26. echo Done.  
    Go to the Solution Explorer the right-click the project then select Properties then go to the Build Events tab.

    Build Events

    Paste the following script in the post-build event command line:

    xcopy "$(ProjectDir)..\Lib\*" "$(TargetDir)" /y /r
    "$(ProjectDir)copyifnewer.bat" "$(ProjectDir)Web.$(ConfigurationName).config" "$(ProjectDir)Web.config"

    Now if you build the project based upon the selection of configuration name , the Web.config file will be updated.


Second Scenario

I have two js (JavaScript) files, and Omniture.Prod.js. When building the project Omniture.Prod.js must be updated with


Paste the following script in the Pre/Post build event command line:

    xcopy "$(ProjectDir)..\Lib\*" "$(TargetDir)" /y /r
    "$(ProjectDir)copyifnewer.bat" "$(ProjectDir)\Scripts\" "$(ProjectDir)\Scripts\Omniture.Prod.js"

Similar Articles