Saturday, December 08, 2007

Post-build event command line

I am working on building a plugin dll to a commercial software product. That dll must be placed in a particular sub-folder within the application's Program Files folder in order to be loaded at application startup.

I want to keep the freshly built plugin.dll and plugin.pdb files in the standard Visual Studio project build folder to be consistent with other projects, but it is a pain to manually copy those two files to the application's reserved folder after each build.

As typical in Visual Studio, when you think "there must be a better way", there usually is.

Go to the project properties, Build Events, and specify a post-build event command line that copies the freshly built assembly to the desired location. Thus it will just automatically be placed there after each successful build. You have the option to execute the post-build event command line even if the build is not successful.

The event command line can be any command that will execute properly in the operating system's command line interface (File, Run, Cmd {enter}). For more complex tasks, you can call a batch file or even a console project executable in your solution.

If the build event fails, your build will fail as well. This is a good thing since you want to be sure that your post-build event completed successfully. A pre-build event command line is available too.

The single line command that i used is below. The /y parameter overwrites any existing file of the same name without prompting. It uses pre-defined project constants that are listed for you when you are in the editor box of that property page. Visual Studio refers to them as "macros".

{commercial software reserved folder location} refers to the specific reserved plugin folder of the software. Note the use of quotes since the build path and destination path may both contain spaces. Note that the macros will expand properly even within quoted strings.

copy /y "$(TargetName).*" "C:\Program Files\{commercial software reserved folder location}\"


Hope that helps.

Joe Kunk
Okemos, MI
USA

2 comments:

Raj Kiran Singh said...

Thanks this helps. I had to copy .Manifest file as .xml at some location after a successfull build.

Mohamed Samir said...

i do it but it is not work the file is empty


Copy "E:\ConfigurationSeparation\ConfigSeparationDemo01\BusinessEntity\bin\Debug\B.dll.config"
"$(ProjectDir)B.dll.config" /y


i want to copy the configuration file to antother configuration file in web application