简体   繁体   English

将文件从一个文件夹复制到根目录

[英]Copy file from one folder to root directory

I have 2 folders named CONFIG1 and CONFIG2 in a Xamarin android project. 我在Xamarin android项目中有2个名为CONFIG1和CONFIG2的文件夹。 Each have one one file(json files) in respective folder. 每个文件在各自的文件夹中都有一个文件(json文件)。 But there is a task during compiling which looks for those specific file(only one) in root directory of project. 但是在编译过程中有一项任务需要在项目的根目录中查找那些特定的文件(只有一个)。 So for the solution I want to copy the file during project build in the root directory by editing the project file. 因此,对于解决方案,我想在项目构建期间通过编辑项目文件将文件复制到根目录中。

I have tried with Copytooutputdirectory and copytopublishdirectory,but none of them working.. 我已经尝试了Copytooutputdirectory和copytopublishdirectory,但是它们都不起作用。

Please help.. 请帮忙..

You could do a pre-build task. 您可以执行预构建任务。 I can't speak to the reliability of the pre-build tasks in Xamarin projects. 我不能说Xamarin项目中预构建任务的可靠性。 Back in the day, they weren't stable. 过去,他们不稳定。 Maybe now they are. 也许现在是。

Here's a picture of the Build Tasks tab (of the project's properties) in Visual Studio 2017, with the "Edit Post Build" dialog open. 这是Visual Studio 2017中(项目属性的)“生成任务”选项卡的图片,其中“编辑后生成”对话框打开。 Basically, the pre- and post-build steps are mechanically identical. 基本上,构建前和构建后的步骤在机械上是相同的。 They run programs in the command shell of your operating system. 它们在操作系统的命令外壳中运行程序。 The "syntax" is just the batch language of the OS. “语法”只是操作系统的批处理语言。 In Windows, for example, you might run a bunch of xcopy commands to move things around. 例如,在Windows中,您可以运行一堆xcopy命令来移动内容。

There's a preprocessor that does variable replacement before running your script...those are what are shown in the "Macros" section...along with their current values. 有一个预处理器在运行脚本之前会进行变量替换...这些是“宏”部分中显示的内容...以及它们的当前值。 The example passes the value of the $(TargetPath) to the update_agent.bat, which is a batch file stored in the root of our solution. 该示例将$(TargetPath)的值传递给update_agent.bat,这是一个存储在解决方案根目录中的批处理文件。 There are a lot of variables to choose from...as I've attempted to show in the dialog box. 正如我试图在对话框中显示的那样,有很多变量可供选择。

Visual Studio的“构建前和构建后”对话框

There are downsides to pre- and post-build steps. 构建前和构建后步骤都有缺点。 They'll resolve differently on different machines...but if you refer to files outside the solution, they may not be on every developer's machine in the same place...and the step will quietly fail. 它们在不同的计算机上的解析方式会有所不同...但是,如果您引用解决方案之外的文件,它们可能不在同一位置的每个开发人员的计算机上...并且此步骤将悄然失败。

Also, they're not portable between operating systems. 而且,它们在操作系统之间不可移植。 I'm not even sure if VS offers pre- and post-build steps on the Mac OS version of Visual Studio. 我什至不确定VS是否在Mac OS版本的Visual Studio上提供了构建前和构建后的步骤。

These downsides are why there are a number of directives supported by the .CSPROJ 这些缺点就是为什么.CSPROJ支持许多指令的.CSPROJ

So, while this is an answer , I doubt it's the answer unless somebody has a better suggestion. 因此,尽管这是一个答案 ,但除非有人有更好的建议,否则我怀疑这是答案 It might get you past your immediate needs, however. 但是,它可能使您摆脱了眼前的需求。

Edit 编辑

MsBuild.exe is the program that's actually processing your .csproj...and you can put MsBuild directives directly in your project. MsBuild.exe是实际上正在处理.csproj ...的程序,您可以将MsBuild指令直接放在项目中。 These directives can use the same set of replaceable variables that are in that pre/post build dialog. 这些指令可以使用该构建前/构建后对话框中的同一组可替换变量。 Might be a better way to go at it. 可能是更好的方法。 Potentially more portable. 可能更便携。

<Target Name="CopyFiles">  
    <Copy  
        SourceFiles="@(MySourceFiles)"  
        DestinationFolder="c:\MyProject\Destination"  
    />  
</Target>  

Here's the Copy task from the MsBuild reference. 这是 MsBuild参考资料中 “复制”任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM