简体   繁体   中英

Copy file from one folder to root directory

I have 2 folders named CONFIG1 and CONFIG2 in a Xamarin android project. Each have one one file(json files) in respective folder. 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..

Please help..

You could do a pre-build task. I can't speak to the reliability of the pre-build tasks in Xamarin projects. 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. 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.

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. 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.

These downsides are why there are a number of directives supported by the .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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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