在编译时将文件复制到应用程序文件夹中

如果我有一些文件,我想从我的项目复制到 .\bin\debug\文件夹编译,那么似乎我必须把它们放在项目的根。将它们放入一个子文件夹似乎是将它们以存储它们的相同结构复制到 .\bin\debug\文件夹中。

有办法避免这种情况吗?

只是要清楚: 如果我有一个 MyFirstConfigFile.txtMySecondConfigFile.txt在一个 ConfigFiles文件夹,我设置他们的 复制到输出收到。,然后他们出现在 .\bin\debug\ConfigFiles\文件夹。我希望它们出现在 .\bin\debug\文件夹中。

134137 次浏览

You can use a MSBuild task on your csproj, like that.

Edit your csproj file

  <Target Name="AfterBuild">
<Copy SourceFiles="$(OutputPath)yourfiles" DestinationFolder="$(YourVariable)" ContinueOnError="true" />
</Target>

You could do this with a post build event. Set the files to no action on compile, then in the macro copy the files to the directory you want.

Here's a post build Macro that I think will work by copying all files in a directory called Configuration to the root build folder:

copy $(ProjectDir)Configuration\* $(ProjectDir)$(OutDir)

You want to use a Post-Build event on your project. You can specify the output there and there are macro values for frequently used things like project path, item name, etc.

You can use the PostBuild event of the project. After the build is completed, you can run a DOS batch file and copy the desired files to your desired folder.

I found this question searching for "copy files into the application folder at compile time". OP seems to have this sorted already, but if you don't:

In Visual Studio right-click the file, select properties, then change the option 'copy to output' to 'always'. See http://msdn.microsoft.com/en-us/library/0c6xyb66.aspx

You can also put the files or links into the root of the solution explorer and then set the files properties:

Build action = Content

and

Copy to Output Directory = Copy if newer (for example)

For a link drag the file from the windows explorer into the solution explorer holding down the shift and control keys.

enter image description here

copy from subfolder to subfolder

 if not exist "$(ProjectDir)$(OutDir)subfolder" mkdir "$(ProjectDir)$(OutDir)subfolder"


copy "$(ProjectDir)subfolder\"  "$(ProjectDir)$(OutDir)subfolder\"

Personally I prefer this way.

Modify the .csproj to add

<ItemGroup>
<ContentWithTargetPath Include="ConfigFiles\MyFirstConfigFile.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>%(Filename)%(Extension)</TargetPath>
</ContentWithTargetPath>
</ItemGroup>

or generalizing, if you want to copy all subfolders and files, you could do:

<ItemGroup>
<ContentWithTargetPath Include="ConfigFiles\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>%(RecursiveDir)\%(Filename)%(Extension)</TargetPath>
</ContentWithTargetPath>
</ItemGroup>