1

I have a config file in my C# project that is excluded from source control (it's local to each developer). However, if the file doesn't exist I want it to be copied from the corresponding ".template" file (which is in source control). To do this, I added an MSBuild item type and target, like this:

  <ItemGroup>
    <AvailableItemName Include="LocalConfigTemplate" />
  </ItemGroup>

  <Target Name="CopyFromTemplateIfNeeded" BeforeTargets="BeforeBuild" Inputs="@(LocalConfigTemplate)" Outputs="@(LocalConfigTemplate->'%(FileName)')">
    <Copy Condition="!Exists(%(LocalConfigTemplate.Filename))" SourceFiles="@(LocalConfigTemplate)" DestinationFiles="@(LocalConfigTemplate->'%(FileName)')" />
  </Target>

(Based on How to hide files generated by custom tool in Visual Studio) This is in a separate file, included from the project file (before Microsoft.CSharp.targets, if that matters).

It works, but when I delete the target file (local.config) and build the project VS thinks it's "up to date" and does not build. How do I get it to detect that the output file is missing and build in that case?

Community
  • 1
  • 1
EM0
  • 5,369
  • 7
  • 51
  • 85

1 Answers1

0

Try doing a Clean before the rebuild, maybe Visual Studio is picking the file up from the bin directory.

  • If I Rebuild it builds (even without Clean), but I want it to also build on Build if the output file is missing. I have tried deleting it from the output directory, too - the project is still considered "up to date". – EM0 Oct 28 '15 at 14:08