using Visual Studio to copy files?

2020-08-13 02:12发布

I want to create a Visual Studio (I am using VSTS 2008) project which simply does file copy work, in more details, I will add some files to this project and this project copy files (included in this project) to some destination location when I build the project.

Any ideas how to do this in VSTS?

BTW: I heard using proj file could do such kinds of task, but I have not found any good simple to learn samples for beginner. :-)

thanks in advance, George

5条回答
淡お忘
2楼-- · 2020-08-13 02:29

The other answers to this question tend to assume you are not using VisualStudio but MSBuild directly or have an existing project for which you can add a custom build step or a build event (I have found build event type of solutions error prone because they will not always copy the files when you need them copied).

If you want a VisualStudio project that only does a copy, you can do this with a stock VisualStudio with a Makefile project.

The slightly misnamed Makefile project can do anything you can do from the command line. Obviously this means you can create a Makefile project that only copies files. In a Makefile project's PropertiesConfiguration PropertiesNMake section are fields for Build, Rebuild, and Clean actions. These can be populated with plain old batch file commands. On every build, VisualStudio will create a temporary batch file from the proper field and execute it with cmd. Warning you will get the 32-bit cmd interpreter on 64-bit machines so you will need to use the magical sysnative directory if you want access 64-bit windows commands or DLLs (I'm not sure if this is documented anywhere so it may not always be the case - your batch scripts should be robust against the bit-ness of cmd changing.

Example batch code requiring 64-bit system files without knowing beforehand what cmd or OS (32-bit or 64-bit) will run the code:

if /i "%programfiles%"=="%programfiles(x86)%" if /i not "%programfiles%"=="%programw6432%" (
    REM 32 bit cmd on 64-bit computer
    REM Use the 'sysnative' fake directory to get to 64-bit system32 files
) else (
    if /i not "%programfiles%"=="%programfiles(x86)%" if /i "%programfiles%"=="%programw6432%" (
        REM 64 bit cmd on 64-bit computer
        REM Use the 'system32' directory to get to 64-bit system32 files
    ) else (
        echo "Cannot do 64-bit on 32-bit computer"
        copy foobar
    )
)

(thie "copy foobar" line above will fail causing the build to fail.)

Note, VisualStudio (and MSBuild) will always run a Makefile project - it does not check the timestamp on the output file but instead punts that job to what it expects to be some other build tool (nmake, cmake, make, ant, maven, etc.). Since, in this case there is just a simple copy command and not some program checking whether or not to perform the copy, the copy occurs on every build.

VisualStudio always running makefile projects also means that every time you press F5 to debug your project, VisualStudio will annoy you with a poorly worded pop up a dialog box (if you haven't told it not to) telling you that you your project is out of date and that you must build it again. If you tell VisualStudio not to display the dialog, it will just perform the copy without asking before running the executable.

查看更多
萌系小妹纸
3楼-- · 2020-08-13 02:30

As mentioned before, Visual Studio .xxproj files are actually MSBuild files. So you can do in them whatever MSBuild allows them to do. I've used them to customize my build process quite a bit. In your case, what you're looking for is the Copy Task. You can add it in the AfterBuild target.

查看更多
The star\"
4楼-- · 2020-08-13 02:31

In Visual Studio 2010, you use Custom Build tools

For example, this rule copies "faq.txt" to the output directory.

<ItemGroup>
  <CustomBuild Include="faq.txt">
    <Message>Copying readme...</Message>
    <Command>copy %(Identity) $(OutDir)%(Identity)</Command>
    <Outputs>$(OutDir)%(Identity)</Outputs>
  </CustomBuild>
</ItemGroup>
查看更多
叛逆
5楼-- · 2020-08-13 02:38

You can add a post build step in Visual Studio:

  • Open the project properties (this works in C# projects, I guess for VB.NET this applies as well).
  • Select the Build Events tab
  • There you can add the post build event commands, for example

    copy $(ProjectDir)\bin\* SOME_OTHER_FOLDER\*

The $(ProjectDir) is a macro, there are a few more available, they will be shown, when you edit a command.

Then, if you have a look at the according project file (XYZ.csproj or XYZ.vbproj), you can see a property group added at the bottom of the file, such as:

  <PropertyGroup>
      <PostBuildEvent>copy $(ProjectDir)\bin\* SOME_OTHER_FOLDER\*</PostBuildEvent>
  </PropertyGroup>

This is how you would do it when directly editing an MSBuild file. Note that you don't need to launch Visual Studio in order to build and have your files copied, you can just pass the project file to the msbuild.exe.

查看更多
男人必须洒脱
6楼-- · 2020-08-13 02:48

The C#/VB.NET project files are MSBuild scripts. MSBuild can do whatever you need...

查看更多
登录 后发表回答