C# – Visual Studio: How to “Copy to Output Directory” without copying the folder structure

cvisual studio 2010

I have a few dll files in \lib folder of my project folder. In the property page of dll, I have selected "Build Action" as "Content" and "Copy to Output Directory" as "Copy always".

After build I am actually getting the dll copied but they are inside \bin\Release\lib and not in \bin\Release.

Is there a way to copy dll files to \bin\Release (and not to \bin\Release\lib) without writing a post-build script or resorting to nant etc?

Best Answer

instead of <Content> use <ContentWithTargetPath> and specify target path, like this:

<ItemGroup>
  <ContentWithTargetPath Include="lib\some_file.dat">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    <TargetPath>some_file.dat</TargetPath>
  </ContentWithTargetPath>
  <None Include="lib\some_file.dat" />
</ItemGroup>

Note that this entry may not be visible from Visual Studio (2012, 2015, 2017), but once manually added to the csproj, it will appear in Visual Studio. The target path will not be editable through the UI though.

Adding a <None> entry for the file will ensure that it still shows up in Visual Studio's UI.

Related Topic