<p>I just finished setting up an out-of-place build system for our existing C++ code using inherited property sheets, a feature that seems to be specific to the Visual C++ product. Building out-of-place requires that many of the project settings be changed, and the inherited property sheets allowed me to change all the necessary settings just by attaching a property sheet to the project. I am migrating our team from C++/MFC for UI to C# and WPF, but I need to provide the same out-of-place build functionality, hopefully with the same convenience. I cannot seem to find a way to do this with C# projects - I first looked to see if I could reference an MsBuild targets file, but could not find a way to do this. I know I could just use MsBuild for the whole thing, but that seems more complicated than necessary. Is there a way I can define a macro for a directory and use it in the output path, for example?</p>

[{'answer_id': 32879, 'author': 'John Smithers', 'author_id': 1069, 'author_profile': 'https://Stackoverflow.com/users/1069', 'pm_score': 0, 'selected': False, 'text': '<blockquote>\n  <p>Is there a way I can define a macro for a directory and use it in the output path  </p>\n</blockquote>\n\n<p>Have you looked at the pre-build and post-build events of a project?</p>\n'}, {'answer_id': 33159, 'author': 'Brian Stewart', 'author_id': 3114, 'author_profile': 'https://Stackoverflow.com/users/3114', 'pm_score': 0, 'selected': False, 'text': '<p>Actually, pre-build and post-build events seem to be solely a place to add batch-file type commands. This would not help me to set up standard build directories for our projects, unfortunately. And having these events create <strong>batch files</strong> seems like a very 1980\'s approach for a modern language like C#, IMO. </p>\n\n<p>After digging some more, and experimenting, I have found that you can add an &lt;Import&gt; directive into your .csproj file. When you do this, the IDE pops up a warning dialog that there is an unsafe entry point in your project - but you can ignore this, and you can make it not appear at all by editing a registry entry, evidently. So this would give me a way to get the variables containing the directory paths I need into the .csproj file. </p>\n\n<p>Now to get the Output Path to refer to it - unfortunately when you add a string like "$(MySpecialPath)/Debug" to the Output Path field, and save the project, the $ and () chars are converted to hex, and your file get\'s put in a Debug directory under a directory named "$(MySpecialPath)". Arrgghh. If you edit the .csproj file in a text editor, you can set this correctly however, and it seems to work as long as the &lt;Import&gt; tag appears before the &lt;PropertyGroup&gt; containing the Output Path. </p>\n\n<p>So I think the solution for me will be to create a standard OurTeam.targets MsBuild file in a standard location, add an installer for changing the registry so it doesn\'t flag warnings, and then create custom project templates that &lt;Import&gt; this file, and also set the Output Path to use the properties defined in the OurTeam.targets file. Sadly, this is more work and a less elegant solution than the property sheet inheritance mechanism in C++.</p>\n'}, {'answer_id': 188237, 'author': 'akmad', 'author_id': 1314, 'author_profile': 'https://Stackoverflow.com/users/1314', 'pm_score': 3, 'selected': True, 'text': '<p>I\'m not quite sure what an "out-of-place" build system is, but if you just need the ability to copy the compiled files (or other resources) to other directories you can do so by tying into the MSBuild build targets.</p>\n\n<p>In our projects we move the compiled dlls into lib folders and put the files into the proper locations after a build is complete.  To do this we\'ve created a custom build .target file that creates the <code>Target</code>\'s, <code>Property</code>\'s, and <code>ItemGroup</code>\'s that we then use to populate our external output folder.</p>\n\n<p>Our custom targets file looks a bit like this:</p>\n\n<pre><code>&lt;Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;\n    &lt;PropertyGroup&gt;\n        &lt;ProjectName&gt;TheProject&lt;/ProjectName&gt;\n        &lt;ProjectDepthPath&gt;..\\..\\&lt;/ProjectDepthPath&gt;\n        &lt;ProjectsLibFolder&gt;..\\..\\lib\\&lt;/ProjectsLibFolder&gt;\n\n        &lt;LibFolder&gt;$(ProjectsLibFolder)$(ProjectName)\\$(Configuration)\\&lt;/LibFolder&gt;\n    &lt;/PropertyGroup&gt;\n\n    &lt;Target Name="DeleteLibFiles"&gt;\n        &lt;Delete Files="@(LibFiles-&gt; \'$(ProjectDepthPath)$(LibFolder)%(filename)%(extension)\')" TreatErrorsAsWarnings="true" /&gt;\n    &lt;/Target&gt;\n    &lt;Target Name="CopyLibFiles"&gt;\n        &lt;Copy SourceFiles="@(LibFiles)" DestinationFolder="$(ProjectDepthPath)$(LibFolder)" SkipUnchangedFiles="True" /&gt;\n    &lt;/Target&gt;\n\n    &lt;ItemGroup&gt;\n        &lt;LibFiles Include=" "&gt;\n            &lt;Visible&gt;false&lt;/Visible&gt;\n        &lt;/LibFiles&gt;\n    &lt;/ItemGroup&gt;\n&lt;/Project&gt;\n</code></pre>\n\n<p>The .csproj file in Visual Studio then integrates with this custom target file:</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;\n&lt;Project ToolsVersion="3.5" ... &gt;\n    ...\n    &lt;Import Project="..\\..\\..\\..\\build\\OurBuildTargets.targets" /&gt;\n      &lt;ItemGroup&gt;\n        &lt;LibFiles Include="$(OutputPath)$(AssemblyName).dll"&gt;\n          &lt;Visible&gt;false&lt;/Visible&gt;\n        &lt;/LibFiles&gt;\n      &lt;/ItemGroup&gt;\n    &lt;Target Name="BeforeClean" DependsOnTargets="DeleteLibFiles" /&gt;\n    &lt;Target Name="AfterBuild" DependsOnTargets="CopyLibFiles" /&gt;\n&lt;/Project&gt;\n</code></pre>\n\n<p>In a nutshell, this build script first tells MSBuild to load our custom build script, then adds the compiled file to the <code>LibFiles</code> ItemGroup, and lastly ties our custom build targets, <code>DeleteLibFiles</code> and <code>CopyLibFiles</code>, into the build process.  We set this up for each project in our solution so only the files that are updated get deleted/copied and each project is responsible for it\'s own files (dlls, images, etc).</p>\n\n<p>I hope this helps.  I apologize if I misunderstood what you mean by out-of-place build system and this is completely useless to you!</p>\n'}]
['https://Stackoverflow.com/questions/32717', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3114/']