C#Dot Net Perls

 
Visual Studio Post-Build and Pre-Build Macros

by Sam Allen

Problem

Use post-build and pre-build macros in Microsoft Visual Studio 2008. You may have two projects that rely on each other but are separate, and may want to keep them separate and work on them independently. For our example, we need a macro to copy files from one project to another after the first one built.

Solution

We use Visual Studio macros for our solution. This approach offers one automated solution to making the two projects work together, at least in a primitive manner. We will copy the compiled DLL to the C# solution's directory. We will use the following command on the post-build event in Visual Studio.

copy "{path1}DLLName.dll" "{path2}DLLName.dll"

Syntax notes. The words including the quotes are actual paths, and they are DOS paths, so they use that funny "\" character. This code runs whenever you build the DLL project. However, let's look at a slightly different solution that is probably a bit better. Let's use the following command in the pre-build event.

copy "{path1}DLLName.dll" "$(ProjectDir)DLLName.dll"

Explanation. Visual Studio 2008 macros are essentially environment variables contained within parentheses, preceded by the $ symbol--much like Perl. (The green italics in the examples must be replaced by you.) You can add a macro to a build command by clicking on its name and clicking insert. Next are a couple example tokens.

$(ProjectDir)
$(TargetFileName)
Shows post and pre-build macros in Visual Studio 2008.

Conclusion

One thing you can use pre-build and post-build events for is copying DLLs to different solutions at build time. This can automate some interactions between different projects. Finally, I want to say that you probably have about 3,000 different things to do right now--do more things automatically.

Dot Net Perls is dedicated to sharing code and knowledge. It has
© 2007-2008 Sam Allen. All rights reserved.

Ads by The Lounge