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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/747941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:38:18  来源:igfitidea点击:

Copying files into the application folder at compile time

c#visual-studio

提问by Andrew Ducker

If I have some files I want to copy from my project into the .\bin\debug\folder on compilation, then it seems I have to put them into the root of the project. Putting them into a subfolder seems to copy them into the .\bin\debug\folder in the same structure they're stored in.

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

Is there any way to avoid this?

有什么办法可以避免这种情况吗?

Just to be clear: if I have a MyFirstConfigFile.txtand MySecondConfigFile.txtin a ConfigFilesfolder and I set their Copy to Outputto be Copy..., then they appear in the .\bin\debug\ConfigFiles\folder. I want them to appear in the .\bin\debug\folder.

需要明确的是:如果我在文件夹中有一个MyFirstConfigFile.txtand并且我将它们的Copy to Output设置为Copy...,那么它们就会出现在文件夹中。我希望它们出现在文件夹中。MySecondConfigFile.txtConfigFiles.\bin\debug\ConfigFiles\.\bin\debug\

采纳答案by JoshBerke

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:

这是一个后期构建宏,我认为它可以通过将名为 Configuration 的目录中的所有文件复制到根构建文件夹来工作:

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

回答by Jhonny D. Cano -Leftware-

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

您可以像这样在 csproj 上使用 MSBuild 任务。

Edit your csproj file

编辑您的 csproj 文件

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

回答by Mark Sherretta

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.

您想在您的项目中使用 Post-Build 事件。您可以在那里指定输出,并且有常用内容的宏值,例如项目路径、项目名称等。

回答by Kirtan

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.

您可以使用项目的 PostBuild 事件。构建完成后,您可以运行 DOS 批处理文件并将所需文件复制到所需文件夹。

回答by Colonel Panic

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:

我发现这个问题正在搜索“在编译时将文件复制到应用程序文件夹中”。OP 似乎已经对此进行了排序,但是如果您不这样做:

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

在 Visual Studio 中右键单击该文件,选择属性,然后将选项“复制到输出”更改为“始终”。请参阅http://msdn.microsoft.com/en-us/library/0c6xyb66.aspx

回答by Georg

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

您还可以将文件或链接放入解决方案资源管理器的根目录,然后设置文件属性:

Build action = Content

Build action = Content

and

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

Copy to Output Directory = Copy if newer(例如)

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

对于链接,按住 shift 和 control 键将文件从 Windows 资源管理器拖到解决方案资源管理器中。

enter image description here

在此处输入图片说明

回答by Ehs?? Kh??

copy from subfolder to subfolder

从子文件夹复制到子文件夹

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

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

回答by Jér?me MEVEL

Personally I prefer this way.

我个人更喜欢这种方式。

Modify the .csprojto add

修改.csproj添加

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