StorageFile.CopyAsync 方法

定义

重载

CopyAsync(IStorageFolder)

在指定文件夹中创建文件的副本。

CopyAsync(IStorageFolder, String)

在指定文件夹中创建文件的副本,并重命名该副本。

CopyAsync(IStorageFolder, String, NameCollisionOption)

在指定文件夹中创建文件的副本,并重命名该副本。 此方法还指定在目标文件夹中已存在同名文件时要执行的操作。

CopyAsync(IStorageFolder)

在指定文件夹中创建文件的副本。

public:
 virtual IAsyncOperation<StorageFile ^> ^ CopyAsync(IStorageFolder ^ destinationFolder) = CopyAsync;
/// [Windows.Foundation.Metadata.Overload("CopyOverloadDefaultNameAndOptions")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<StorageFile> CopyAsync(IStorageFolder const& destinationFolder);
[Windows.Foundation.Metadata.Overload("CopyOverloadDefaultNameAndOptions")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder);
function copyAsync(destinationFolder)
Public Function CopyAsync (destinationFolder As IStorageFolder) As IAsyncOperation(Of StorageFile)

参数

destinationFolder
IStorageFolder

在其中创建文件副本的目标文件夹。

返回

此方法完成后,它将返回一个 StorageFile ,表示在 destinationFolder 中创建的文件的副本。

实现

属性

例外

您没有将文件复制到目标文件夹的权限。

示例

以下示例将文件复制到指定文件夹,并通过调用 CopyAsync (IStorageFolder、String、NameCollisionOption) 方法重命名副本。 此示例显式为 选项 指定一个值,如果已存在具有指定名称的文件,则替换目标文件夹中的文件。

using Windows.Storage;
using System.Threading.Tasks;

// Get the app's temporary folder.
StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;

// Create a sample file in the temporary folder.
string newFileName = "test.txt";
StorageFile newFile = await tempFolder.CreateFileAsync(newFileName);

// Get the app's local folder to use as the destination folder.
StorageFolder localFolder = ApplicationData.Current.LocalFolder;

// Specify a new name for the copied file.
string renamedFileName = "renamed_test.txt";

// Copy the file to the destination folder and rename it.
// Replace the existing file if the file already exists.
StorageFile copiedFile = await newFile.CopyAsync(localFolder, renamedFileName, NameCollisionOption.ReplaceExisting);
IAsyncAction MainPage::CopyFileAsync()
{
    // Get the app's temporary folder.
    StorageFolder sourceFolder{ ApplicationData::Current().TemporaryFolder() };
    StorageFolder targetFolder{ ApplicationData::Current().LocalFolder() };

    // Choose source file name and new name for the copied file.
    std::wstring newFileName{ L"test.txt" };
    std::wstring copyFileName{ L"renamed_test.txt" };

    // Create a sample file in the temporary folder.
    StorageFile sourceFile{ co_await sourceFolder.CreateFileAsync(newFileName, Windows::Storage::CreationCollisionOption::ReplaceExisting) };
    // Overwrite any existing file with the same name.
    StorageFile copiedFile{ co_await sourceFile.CopyAsync(targetFolder, copyFileName, Windows::Storage::NameCollisionOption::ReplaceExisting) };
    // Do something with copied file.
}
//Get the app's temporary folder
StorageFolder^ sourceFolder = ApplicationData::Current->TemporaryFolder;
StorageFolder^ targetFolder = ApplicationData::Current->LocalFolder;

// Choose source file name and new name for the copied file
String^ newFileName = "test.txt";
String^ copyFileName = "renamed_test.txt";

//Create a sample file in the temporary folder
auto copyFileTask = create_task(sourceFolder->CreateFileAsync(newFileName, Windows::Storage::CreationCollisionOption::ReplaceExisting)).then
([targetFolder, copyFileName](StorageFile^ sourceFile) -> task<StorageFile^>
{
    //Overwrite any existing file with the same name 
    auto copyFileTask = sourceFile->CopyAsync(targetFolder, copyFileName, Windows::Storage::NameCollisionOption::ReplaceExisting);
    return create_task(copyFileTask);
}).then([](StorageFile^ copiedFile) {
    //do something with copied file
});

注解

默认情况下,此方法使用 CreationCollisionOption 枚举中的 FailIfExists 值。 也就是说,如果目标文件夹中已存在同名的文件,此方法将引发异常。 如果要以其他方式处理文件名冲突,请调用 CopyAsync (IStorageFolder、String、NameCollisionOption) 重载方法。

注意

使用 StorageFile.CopyAsync 将加密的文件复制到未加密的目标时,调用将失败,并出现以下异常: System.UnauthorizedAccessException: Access is denied. (Excep_FromHResult 0x80070005)

另请参阅

适用于

CopyAsync(IStorageFolder, String)

在指定文件夹中创建文件的副本,并重命名该副本。

public:
 virtual IAsyncOperation<StorageFile ^> ^ CopyAsync(IStorageFolder ^ destinationFolder, Platform::String ^ desiredNewName) = CopyAsync;
/// [Windows.Foundation.Metadata.Overload("CopyOverloadDefaultOptions")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<StorageFile> CopyAsync(IStorageFolder const& destinationFolder, winrt::hstring const& desiredNewName);
[Windows.Foundation.Metadata.Overload("CopyOverloadDefaultOptions")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder, string desiredNewName);
function copyAsync(destinationFolder, desiredNewName)
Public Function CopyAsync (destinationFolder As IStorageFolder, desiredNewName As String) As IAsyncOperation(Of StorageFile)

参数

destinationFolder
IStorageFolder

在其中创建文件副本的目标文件夹。

desiredNewName
String

Platform::String

winrt::hstring

在 destinationFolder 中创建的文件副本的新名称。

返回

此方法完成后,它将返回一个 StorageFile ,表示在 destinationFolder 中创建的文件的副本。

注意

使用 StorageFile.CopyAsync 将加密的文件复制到未加密的目标时,调用将失败,并出现以下异常: System.UnauthorizedAccessException: Access is denied. (Excep_FromHResult 0x80070005)

实现

M:Windows.Storage.IStorageFile.CopyAsync(Windows.Storage.IStorageFolder,System.String) M:Windows.Storage.IStorageFile.CopyAsync(Windows.Storage.IStorageFolder,Platform::String) M:Windows.Storage.IStorageFile.CopyAsync(Windows.Storage.IStorageFolder,winrt::hstring)
属性

例外

您没有将文件复制到目标文件夹的权限。

另请参阅

适用于

CopyAsync(IStorageFolder, String, NameCollisionOption)

在指定文件夹中创建文件的副本,并重命名该副本。 此方法还指定在目标文件夹中已存在同名文件时要执行的操作。

public:
 virtual IAsyncOperation<StorageFile ^> ^ CopyAsync(IStorageFolder ^ destinationFolder, Platform::String ^ desiredNewName, NameCollisionOption option) = CopyAsync;
/// [Windows.Foundation.Metadata.Overload("CopyOverload")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<StorageFile> CopyAsync(IStorageFolder const& destinationFolder, winrt::hstring const& desiredNewName, NameCollisionOption const& option);
[Windows.Foundation.Metadata.Overload("CopyOverload")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder, string desiredNewName, NameCollisionOption option);
function copyAsync(destinationFolder, desiredNewName, option)
Public Function CopyAsync (destinationFolder As IStorageFolder, desiredNewName As String, option As NameCollisionOption) As IAsyncOperation(Of StorageFile)

参数

destinationFolder
IStorageFolder

在其中创建文件副本的目标文件夹。

desiredNewName
String

Platform::String

winrt::hstring

在 destinationFolder 中创建的文件副本的新名称。

option
NameCollisionOption

如果目标文件夹中已存在具有指定 desiredNewName 的文件,则确定如何处理冲突的枚举值之一。

返回

此方法完成后,它将返回一个 StorageFile ,表示在 destinationFolder 中创建的文件的副本。

注意

使用 StorageFile.CopyAsync 将加密的文件复制到未加密的目标时,调用将失败,并出现以下异常: System.UnauthorizedAccessException: Access is denied. (Excep_FromHResult 0x80070005)

实现

M:Windows.Storage.IStorageFile.CopyAsync(Windows.Storage.IStorageFolder,System.String,Windows.Storage.NameCollisionOption) M:Windows.Storage.IStorageFile.CopyAsync(Windows.Storage.IStorageFolder,Platform::String,Windows.Storage.NameCollisionOption) M:Windows.Storage.IStorageFile.CopyAsync(Windows.Storage.IStorageFolder,winrt::hstring,Windows.Storage.NameCollisionOption)
属性

例外

您没有将文件复制到目标文件夹的权限。

另请参阅

适用于