Far to often I've had multi-project solutions in Visual Studio not build because of post build copy tasks. Far too often I've seen references to the parent directory of a project to get files. No doubt both these things have a time and place; you might have a Javascript minify task in your post build or be writing an application that needs to access data or configuration from a parent directory.

Most of the time this is not the case and I often see people overlooking the "copy to output directory" property on items in the solution explorer. This property is really useful to solve the common problem of needing resources from your project in the output, and it works in multi-project solutions!

An example scenario would be if you had a solution with an application project, and assembly project and a test assembly project. In this scenario the assembly project has an XSL transform file, it makes sense to have this file with the code in the project. Both the test project and the application project will need to know about this file.

I've actually seen code that references the file in project location, using lots of ".." and "\" in the path to reference the source file. This will cause problems if your application actually modifies the source code. It will cause problems if you restructure your solution. And if you use absolute paths it will prevent the solution from building unless its in a specific location.

I've also seen extensive post build tasks that copy the required files to all the places that might need them. This is means that every new project the references the assembly will needed to be added in the post-build copy steps.

Both these solutions have maintenance problems and are excessively difficult to implement. The best solution here is to use the "copy to output directory" property on the XSL file in the solution explorer. This will mean that when you build your solution it will be copied to the output of all projects in the solution that reference the project.

There is a "copy always" and a "copy if newer" option. As I said earlier there is a time and place for the other methods so it's best to think through what makes sense for your solution.

Comments

Comment