Copy 工作Copy task

將檔案複製到檔案系統上的新位置。Copies files to a new location in the file system.

參數Parameters

下表說明 Copy 工作的參數。The following table describes the parameters of the Copy task.

參數Parameter 說明Description
CopiedFiles 選擇性的 ITaskItem[] 輸出參數。Optional ITaskItem[] output parameter.

包含已成功複製的項目。Contains the items that were successfully copied.
DestinationFiles 選擇性的 ITaskItem[] 參數。Optional ITaskItem[] parameter.

指定要將來源檔案複製到其中的檔案清單。Specifies the list of files to copy the source files to. 此清單與 SourceFiles 參數中指定的清單應該是一對一對應。This list is expected to be a one-to-one mapping with the list specified in the SourceFiles parameter. 也就是,會將 SourceFiles 中指定的第一個檔案複製到 DestinationFiles 中指定的第一個位置,依此類推。That is, the first file specified in SourceFiles will be copied to the first location specified in DestinationFiles, and so forth.
DestinationFolder 選擇性的 ITaskItem 參數。Optional ITaskItem parameter.

指定要將檔案複製至其中的目錄。Specifies the directory to which you want to copy the files. 這必須是目錄,而非檔案。This must be a directory, not a file. 如果目錄不存在,即會自動建立。If the directory does not exist, it is created automatically.
OverwriteReadOnlyFiles 選擇性的 Boolean 參數。Optional Boolean parameter.

即使已將檔案標示為唯讀檔案,還是會覆寫它們Overwrite files even if they are marked as read only files
Retries 選擇性的 Int32 參數。Optional Int32 parameter.

指定如果所有先前的嘗試均失敗,要嘗試複製多少次。Specifies how many times to attempt to copy, if all previous attempts have failed. 預設值為零。Defaults to zero.

注意: 使用重試,可以為建置流程中的同步處理問題設定遮罩。Note: The use of retries can mask a synchronization problem in your build process.
RetryDelayMilliseconds 選擇性的 Int32 參數。Optional Int32 parameter.

指定任何必要重試之間的延遲。Specifies the delay between any necessary retries. 預設值為 RetryDelayMillisecondsDefault 引數,其會傳遞至 CopyTask 建構函式。Defaults to the RetryDelayMillisecondsDefault argument, which is passed to the CopyTask constructor.
SkipUnchangedFiles 選擇性的 Boolean 參數。Optional Boolean parameter.

如果是 true,即會略過複製來源與目的地之間未變更的檔案。If true, skips the copying of files that are unchanged between the source and destination. 如果檔案具有相同的大小和相同的上次修改時間,Copy 工作即會將檔案視為未變更。The Copy task considers files to be unchanged if they have the same size and the same last modified time.

注意: 如果您將此參數設為 true,您應該在包含目標上使用相依性分析,因為只有在來源檔案的上次修改時間比目的地檔案的上次修改時間還新時,才會執行此工作。Note: If you set this parameter to true, you should not use dependency analysis on the containing target, because that only runs the task if the last-modified times of the source files are newer than the last-modified times of the destination files.
SourceFiles 必要的 ITaskItem[] 參數。Required ITaskItem[] parameter.

指定要複製的檔案。Specifies the files to copy.
UseHardlinksIfPossible 選擇性的 Boolean 參數。Optional Boolean parameter.

如果是 true,即會針對複製的檔案建立永久連結,而非複製檔案。If true, creates Hard Links for the copied files instead of copying the files.

警告Warnings

系統會記錄警告,包括:Warnings are logged, including:

  • Copy.DestinationIsDirectory

  • Copy.SourceIsDirectory

  • Copy.SourceFileNotFound

  • Copy.CreatesDirectory

  • Copy.HardLinkComment

  • Copy.RetryingAsFileCopy

  • Copy.FileComment

  • Copy.RemovingReadOnlyAttribute

備註Remarks

您必須指定 DestinationFolderDestinationFiles 參數,但不能同時指定這兩者。Either the DestinationFolder or the DestinationFiles parameter must be specified, but not both. 如果同時指定這兩者,工作即會失敗,並記錄錯誤。If both are specified, the task fails and an error is logged.

除了上述所列的參數,此項工作還會繼承 TaskExtension 類別中的參數,而該類別本身又繼承 Task 類別。In addition to the parameters listed above, this task inherits parameters from the TaskExtension class, which itself inherits from the Task class. 如需這些其他參數的清單及其描述,請參閱 TaskExtension 基底類別For a list of these additional parameters and their descriptions, see TaskExtension base class.

範例Example

下列範例會將 MySourceFiles 項目集合中的項目複製到資料夾 c:\MyProject\DestinationThe following example copies the items in the MySourceFiles item collection into the folder c:\MyProject\Destination.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <ItemGroup>
        <MySourceFiles Include="a.cs;b.cs;c.cs"/>
    </ItemGroup>

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

</Project>

範例Example

下列範例示範如何進行遞迴複製。The following example demonstrates how to do a recursive copy. 此專案會以遞迴方式將所有檔案從 c:\MySourceTree 中複製到 c:\MyDestinationTree,同時保有目錄結構。This project copies all of the files recursively from c:\MySourceTree into c:\MyDestinationTree, while maintaining the directory structure.

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <ItemGroup>
        <MySourceFiles Include="c:\MySourceTree\**\*.*"/>
    </ItemGroup>

    <Target Name="CopyFiles">
        <Copy
            SourceFiles="@(MySourceFiles)"
            DestinationFiles="@(MySourceFiles->'c:\MyDestinationTree\%(RecursiveDir)%(Filename)%(Extension)')"
        />
    </Target>

</Project>

另請參閱See also