使用Visual Studio复制文件?

时间:2023-01-14 19:11:52

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.

我想创建一个Visual Studio(我正在使用VSTS 2008)项目,它只是简单地进行文件复制工作,更详细地说,我将向这个项目添加一些文件,并将此项目复制文件(包含在此项目中)复制到某个目标位置我建立了这个项目。

Any ideas how to do this in VSTS?

如何在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. :-)

BTW:我听说使用proj文件可以完成这样的任务,但我还没有找到任何好的简单学习初学者的样本。 :-)

thanks in advance, George

乔治,提前谢谢

5 个解决方案

#1


13  

You can add a post build step in Visual Studio:

您可以在Visual Studio中添加后期构建步骤:

  • Open the project properties (this works in C# projects, I guess for VB.NET this applies as well).
  • 打开项目属性(这适用​​于C#项目,我想对VB.NET也适用)。
  • Select the Build Events tab
  • 选择“构建事件”选项卡
  • There you can add the post build event commands, for example

    例如,您可以在其中添加post build事件命令

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

    复制$(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.

$(ProjectDir)是一个宏,当您编辑命令时,还会显示一些宏,它们将被显示。

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:

然后,如果您查看相应的项目文件(XYZ.csproj或XYZ.vbproj),则可以在文件底部看到添加的属性组,例如:

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

这是直接编辑MSBuild文件时的方法。请注意,您无需启动Visual Studio即可构建并复制文件,您只需将项目文件传递给msbuild.exe即可。

#2


4  

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.

如前所述,Visual Studio .xxproj文件实际上是MSBuild文件。所以无论MSBuild允许他们做什么,你都可以做到。我已经用它们来定制我的构建过程了。在您的情况下,您正在寻找的是复制任务。您可以在AfterBuild目标中添加它。

#3


0  

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

C#/ VB.NET项目文件是MSBuild脚本。 MSBuild可以做任何你需要的事情......

#4


0  

In Visual Studio 2010, you use Custom Build tools

在Visual Studio 2010中,您使用自定义生成工具

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

例如,此规则将“faq.txt”复制到输出目录。

<ItemGroup>
  <CustomBuild Include="faq.txt">
    <Message>Copying readme...</Message>
    <Command>copy %(Identity) $(OutDir)%(Identity)</Command>
    <Outputs>$(OutDir)%(Identity)</Outputs>
  </CustomBuild>
</ItemGroup>

#5


0  

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

这个问题的其他答案倾向于假设您没有直接使用VisualStudio而是使用MSBuild,或者有一个现有的项目,您可以为其添加自定义构建步骤或构建事件(我发现构建事件类型的解决方案容易出错,因为它们不会总是在需要复制文件时复制文件)。

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

如果您想要一个仅复制的VisualStudio项目,您可以使用带有Makefile项目的VisualStudio库存来完成此操作。

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.

稍微命名错误的Makefile项目可以从命令行执行任何操作。显然,这意味着您可以创建仅复制文件的Makefile项目。在Makefile项目的Properties⇒ConfigurationProperties⇒NMake部分是Build,Rebuild和Clean操作的字段。这些可以使用普通的旧批处理文件命令填充。在每次构建时,VisualStudio将从适当的字段创建临时批处理文件并使用cmd执行它。警告您将在64位计算机上获得32位cmd解释器,因此如果要访问64位Windows命令或DLL,则需要使用神奇的sysnative目录(我不确定这是否记录在任何地方,因此可能并非总是如此 - 您的批处理脚本应该能够抵御cmd更改的位置。

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

需要64位系统文件的示例批处理代码,而不事先知道cmd或OS(32位或64位)将运行代码:

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

(上面的“复制foobar”行将导致构建失败。)

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(和MSBuild)将始终运行Makefile项目 - 它不会检查输出文件上的时间戳,而是将该作业归结为它期望的其他构建工具(nmake,cmake,make,ant,maven,等等。)。因为,在这种情况下,只有一个简单的复制命令,而不是某些程序检查是否执行复制,所以复制发生在每个构建上。

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.

VisualStudio始终运行makefile项目也意味着每次按F5调试项目时,VisualStudio会用一个措辞不好的弹出一个对话框(如果你没有告诉它)告诉你你的项目已经出来的日期,你必须再次建立它。如果告诉VisualStudio不显示对话框,它将在运行可执行文件之前执行副本而不询问。

#1


13  

You can add a post build step in Visual Studio:

您可以在Visual Studio中添加后期构建步骤:

  • Open the project properties (this works in C# projects, I guess for VB.NET this applies as well).
  • 打开项目属性(这适用​​于C#项目,我想对VB.NET也适用)。
  • Select the Build Events tab
  • 选择“构建事件”选项卡
  • There you can add the post build event commands, for example

    例如,您可以在其中添加post build事件命令

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

    复制$(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.

$(ProjectDir)是一个宏,当您编辑命令时,还会显示一些宏,它们将被显示。

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:

然后,如果您查看相应的项目文件(XYZ.csproj或XYZ.vbproj),则可以在文件底部看到添加的属性组,例如:

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

这是直接编辑MSBuild文件时的方法。请注意,您无需启动Visual Studio即可构建并复制文件,您只需将项目文件传递给msbuild.exe即可。

#2


4  

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.

如前所述,Visual Studio .xxproj文件实际上是MSBuild文件。所以无论MSBuild允许他们做什么,你都可以做到。我已经用它们来定制我的构建过程了。在您的情况下,您正在寻找的是复制任务。您可以在AfterBuild目标中添加它。

#3


0  

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

C#/ VB.NET项目文件是MSBuild脚本。 MSBuild可以做任何你需要的事情......

#4


0  

In Visual Studio 2010, you use Custom Build tools

在Visual Studio 2010中,您使用自定义生成工具

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

例如,此规则将“faq.txt”复制到输出目录。

<ItemGroup>
  <CustomBuild Include="faq.txt">
    <Message>Copying readme...</Message>
    <Command>copy %(Identity) $(OutDir)%(Identity)</Command>
    <Outputs>$(OutDir)%(Identity)</Outputs>
  </CustomBuild>
</ItemGroup>

#5


0  

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

这个问题的其他答案倾向于假设您没有直接使用VisualStudio而是使用MSBuild,或者有一个现有的项目,您可以为其添加自定义构建步骤或构建事件(我发现构建事件类型的解决方案容易出错,因为它们不会总是在需要复制文件时复制文件)。

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

如果您想要一个仅复制的VisualStudio项目,您可以使用带有Makefile项目的VisualStudio库存来完成此操作。

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.

稍微命名错误的Makefile项目可以从命令行执行任何操作。显然,这意味着您可以创建仅复制文件的Makefile项目。在Makefile项目的Properties⇒ConfigurationProperties⇒NMake部分是Build,Rebuild和Clean操作的字段。这些可以使用普通的旧批处理文件命令填充。在每次构建时,VisualStudio将从适当的字段创建临时批处理文件并使用cmd执行它。警告您将在64位计算机上获得32位cmd解释器,因此如果要访问64位Windows命令或DLL,则需要使用神奇的sysnative目录(我不确定这是否记录在任何地方,因此可能并非总是如此 - 您的批处理脚本应该能够抵御cmd更改的位置。

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

需要64位系统文件的示例批处理代码,而不事先知道cmd或OS(32位或64位)将运行代码:

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

(上面的“复制foobar”行将导致构建失败。)

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(和MSBuild)将始终运行Makefile项目 - 它不会检查输出文件上的时间戳,而是将该作业归结为它期望的其他构建工具(nmake,cmake,make,ant,maven,等等。)。因为,在这种情况下,只有一个简单的复制命令,而不是某些程序检查是否执行复制,所以复制发生在每个构建上。

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.

VisualStudio始终运行makefile项目也意味着每次按F5调试项目时,VisualStudio会用一个措辞不好的弹出一个对话框(如果你没有告诉它)告诉你你的项目已经出来的日期,你必须再次建立它。如果告诉VisualStudio不显示对话框,它将在运行可执行文件之前执行副本而不询问。