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)
屬性

例外狀況

您沒有將檔案複製到目的地資料夾的許可權。

另請參閱

適用於