Copy to Output Directory copies folder structure b

2020-01-24 11:15发布

I have a VS2008 I want to copy certain files from a directory into my /bin/ folder. I have set the files (located in /common/browserhawk/) to "Copy to Output Directory". However, it copies the folder structure as well: the files are copied to /bin/common/browserhawk/

How do I get these files to copy to just /bin/? I do not want to store these in the root of the website to get them to copy correctly.

Related Question: Visual Studio adds .dll and .pdb to project after compiling

8条回答
女痞
2楼-- · 2020-01-24 11:31

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.

查看更多
Ridiculous、
3楼-- · 2020-01-24 11:35

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.

查看更多
我命由我不由天
4楼-- · 2020-01-24 11:35

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

查看更多
够拽才男人
5楼-- · 2020-01-24 11:37

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
查看更多
不美不萌又怎样
6楼-- · 2020-01-24 11:42

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.

查看更多
男人必须洒脱
7楼-- · 2020-01-24 11:48

I have used this in VS2010 and VS2015. 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>False</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

查看更多
登录 后发表回答