复制到输出目录复制文件夹结构,但只想复制文件

我有一个 VS2008我想复制某些文件从一个目录到我的 /bin/文件夹。我已经将文件(位于 /common/browserhawk/中)设置为“复制到输出目录”。但是,它也复制文件夹结构: 文件被复制到 /bin/common/browserhawk/

如何将这些文件复制到 /bin/?我不想存储这些在网站的根让他们复制正确。

相关问题: VisualStudio 在编译后将. dll 和. pdb 添加到项目中

60220 次浏览

You could build a batch file to copy the files and execute it as a post-build event.

Add the following to your .csproj/.vbproj file

<Target Name="AfterBuild">
<Copy
DestinationFolder="$(OutputPath)"
SourceFiles="@(RootContent)"
SkipUnchangedFiles="true"
/>
</Target>

Then change the Build Action of any files you want in the root folder to RootContent.

I ended up adding a step to the nant build file to copy after successful compliation

<target name="action.copy.browserhawk.config" depends="compile.source">
<copy todir="${App.Web.dir}/bin/" includeemptydirs="false">
<fileset basedir="${browserhawk.config.dir}">
<include name="bhawk_bb.dat" />
<include name="bhawk_sp.dat" />
<include name="browserhawk.properties" />
<include name="maindefs.bdd" />
<include name="maindefs.bdf" />
<include name="BH_PRO.lic" />
</fileset>
</copy>
<echo message="COPY BROWSERHAWK CONFIG: SUCCESS   ${datetime::now()}" />
</target>

You can add a Post Build Event to copy the files.
Go to project properties, Build Events tab and add the following to the Post-build event command line:

copy "$(ProjectDir)\common\browserhawk\*.*" "$(TargetDir)"

Be sure to include the quotes if your project path has spaces in it.

I believe the XCOPY command handles directories and files better. Therefore,

    XCOPY "$(ProjectDir)common/browserhawk" "$(TargetDir)" /E /I /F /Y

Which allows for creating folders off the target directory.

    XCOPY "$(ProjectDir)Templates" "$(TargetDir)" /E /I /F /Y

The Project folder/file structure of:

    A:\TEMP\CONSOLEAPPLICATION3\TEMPLATES
├───NewFolder1
├───NewFolder2
│       TextFile1.txt
│       TextFile2.txt
└───NewFolder3
TextFile1.txt
TextFile2.txt
TextFile3.txt

Becomes:

    A:\TEMP\CONSOLEAPPLICATION3\BIN\DEBUG
│   ConsoleApplication3.exe
│   ConsoleApplication3.pdb
│   ConsoleApplication3.vshost.exe
│   ConsoleApplication3.vshost.exe.manifest
├───NewFolder1
├───NewFolder2
│       TextFile1.txt
│       TextFile2.txt
│
└───NewFolder3
TextFile1.txt
TextFile2.txt
TextFile3.txt

Since I cannot comment on previous answers, I will put the solution here:

Adding to what @PaulAlexander provided, add the following to your .csproj/.vbproj file:

<ItemGroup>
<AvailableItemName Include="RootContent">
<Visible>false</Visible>
</AvailableItemName>
</ItemGroup>
<Target Name="AfterBuild">
<Copy
DestinationFolder="$(OutputPath)"
SourceFiles="@(RootContent)"
SkipUnchangedFiles="true"
/>
</Target>

This allows you to select "RootContent" as the Build Action in the Properties window, and all can be accessed via the GUI. A more complete explanation: the "AvailableItemName" option basically creates a new named-list that you can assign items in the project to under the "Build Action" property in the Properties window. You can then use this newly created list in any Target you like (eg via "@(RootContent)").

If you edit the .csproj / .vbproj in a text editor, you can control where the file is placed in the output directory, and also what name the file will have in the output directory. For example:

<None Include="content\someContent.txt">
<Link>someContentInOutputDirectory.txt</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

This will put the file content\someContent.txt into bin\someContentInOutputDirectory.txt. You can also choose a subdirectory in bin if you want; simply add it to the Link element.

I have used this in VS2010, VS2015, VS2017, and VS2019. I prefer this solution because:

  1. The XML is reusable in any project
  2. The "RootContent" is chosen as a Build Action in the Visual Studio UI, just like any other "Content"
  3. The "CopyToOutputDirectory" is obeyed, just as you would expect
  4. The RootContent is added to the project's output: gets carried through Project-References, obeys "Clean", etc.
  5. The RootContent can be specified with a wildcard, preserving the recursive folder structure:
<RootContent Include="common\browserhawk\**">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</RootContent>

Toward the beginning of project file:

  <ItemGroup>
<AvailableItemName Include="RootContent">
<!-- Add "RootContent" as a choice in the "Build Action" dropdown. -->
<Visible>True</Visible>
</AvailableItemName>
</ItemGroup>

Borrowed From This Answer

After the Microsoft .targets Import:

  <PropertyGroup>
<AssignTargetPathsDependsOn>
$(AssignTargetPathsDependsOn);
IncludeRootContentAsContent;
</AssignTargetPathsDependsOn>
</PropertyGroup>
<Target Name="IncludeRootContentAsContent">
<CreateItem Include="@(RootContent)" AdditionalMetadata="TargetPath=%(RecursiveDir)%(Filename)%(Extension)">
<Output ItemName="ContentWithTargetPath" TaskParameter="Include" />
</CreateItem>
</Target>

Borrowed From This Answer