Tuesday, April 17, 2012

How to use Pre-build and Post-build Events in Visual Studio?



Many of you might aware about Build Events feature of Visual Studio. In this post i will explain little bit about Pre-build and Post-build events and their usage.

Sometimes you might need to execute some additional tasks just before and after building your Visual Studio solution. So for this Visual Studio provides nice way to do that using Build Events. You can specify commands inside pre-build and post-build command line. You can find build events tab inside project properties.


As mentioned in above image, you can specify almost all DOS commands inside pre-build and post-build event as per your requirement. Post-build event gives you more control over Pre-build command line execution. You can decide when to execute post-build event from given three options displayed in above image.

Let’s try to understand with simple example.



On successful build I want to copy my executable to some specific folder on my local drive. So in above example I have used xcopy command to copy files from one directory to another. In above example I have used ‘$(ProjectDir)’ macro which will gives me the current project directory. There are more macros available to use for various purposes see below image. These macros can be used to specify location of project or solution, project name, extension, output directory, configuration name etc.



You can use this feature in many scenarios like processing or executing batch file, copy assemblies to specific location, generate resources file using tool like resgen.exe etc. This is really nice and useful feature provided by visual studio.

Hope you liked this small tip. Please leave your feedback in comments section.


See Also – 

2 comments:

  1. I have a simple example that We generally require to run the Application(Desktop Application) under Admin permission.

    ReplyDelete
  2. this command is not working in post build event of project
    resgen "$(ProjectDir)SpectrumLite_en-US.txt" "$(ProjectDir)"

    error message in output window is

    'resgen' is not recognized as an internal or external command,
    operable program or batch file.

    that command successfully run on visual stdio command prompt but nighter in cmd nor post build event .

    ReplyDelete