Q. How do I make one project item, say an XML file, be copied to the output folder when the project is compiled? My code assumes that the file is in the same folder as the compiled executable. I want to avoid having to manually copy that file every time.

Asked by reader. Answered by the Wonk on November 18, 2002

A.

VS.NET has this great feature that takes an app.config file in the root of a project and copies it to the output of the project at build-time, renaming the file to <projectName>.exe.config, as per .NET requirements. However, it doesn’t do that for any other type of file, e.g. app.manifest. Likewise, VS.NET 2002 provides no capacity in C# or VB.NET projects to do anything custom at different parts of the project build cycle, like pre-build or post-build, where you could do these kinds of things yourself. If you happen to have a version of VS.NET that provides C++ project types, you can create a C++ Makefile project, change it to a Utility project, add your shell commands in pre-build or post-build events and, using VS.NET build dependencies, make your C++ Utility project execute at the right time during the build process.

 

However, as you might be able to tell from the description, this is a bit of a hack. Luckily, in VS.NET 2003, the latest version that supports the .NET Framework 1.1 and available in beta version to MSDN subscribers, C# projects allow you to add your shell commands to the pre-build and post-build events during the project build. Access to the project build event settings are available via Project->Properties->Common Properties->Build Events. Figure 1 shows the copying of an app.manifest file to the output directory based on the project name.

 

[Image]

Figure 1: VS.NET 2003 C# project pre-build and post-build event handling

Notice the use of variables in Figure 1:

 

copy $(ProjectDir)app.manifest $(ProjectDir)$(OutDir)$(ProjectName)$(TargetExt).manifest

 

You can type these in from memory, or you can press the “…” button to show the event command line editor dialog, as shown in Figure 2.

 

[Image]

Figure 2: Build event command line editor dialog

And as if the event builder dialog wasn’t enough, the batch file being executed will be written to the output directory of the project, so you can debug exactly what is being executed if there’re any problems.

Feedback

I have feedback on this Ask The Wonk answer