StorageFile.CopyAsync StorageFile.CopyAsync StorageFile.CopyAsync StorageFile.CopyAsync StorageFile.CopyAsync Method

Definition

Overloads

CopyAsync(IStorageFolder) CopyAsync(IStorageFolder) CopyAsync(IStorageFolder) CopyAsync(IStorageFolder) CopyAsync(IStorageFolder)

Creates a copy of the file in the specified folder.

CopyAsync(IStorageFolder, String) CopyAsync(IStorageFolder, String) CopyAsync(IStorageFolder, String) CopyAsync(IStorageFolder, String) CopyAsync(IStorageFolder, String)

Creates a copy of the file in the specified folder and renames the copy.

CopyAsync(IStorageFolder, String, NameCollisionOption) CopyAsync(IStorageFolder, String, NameCollisionOption) CopyAsync(IStorageFolder, String, NameCollisionOption) CopyAsync(IStorageFolder, String, NameCollisionOption) CopyAsync(IStorageFolder, String, NameCollisionOption)

Creates a copy of the file in the specified folder and renames the copy. This method also specifies what to do if a file with the same name already exists in the destination folder.

CopyAsync(IStorageFolder) CopyAsync(IStorageFolder) CopyAsync(IStorageFolder) CopyAsync(IStorageFolder) CopyAsync(IStorageFolder)

Creates a copy of the file in the specified folder.

public : IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder)
IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder) const;
public IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder)
Public Function CopyAsync(destinationFolder As IStorageFolder) As IAsyncOperation(Of StorageFile)
Windows.Storage.StorageFile.copyAsync(destinationFolder).done( /* Your success and error handlers */ );

Parameters

destinationFolder
IStorageFolder IStorageFolder IStorageFolder

The destination folder where the copy of the file is created.

Returns

When this method completes, it returns a StorageFile that represents the copy of the file created in the destinationFolder.

Exceptions

UnauthorizedAccessException UnauthorizedAccessException UnauthorizedAccessException

You don't have permission to copy a file into the destination folder.

Examples

The following example copies a file to the specified folder and renames the copy by calling the CopyAsync(IStorageFolder, String, NameCollisionOption) method. This example explicitly specifies a value for option that replaces the file in the destination folder if a file with the specified name already exists.

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
});
// Get the app's temporary folder.
var ApplicationData = Windows.Storage.ApplicationData;
var tempFolder = ApplicationData.current.temporaryFolder;

// Create a sample file in the temporary folder.
var newFileName = "test.txt";
var newFilePromise = tempFolder.createFileAsync(newFileName);
var copiedFilePromise = newFilePromise.then(function (newFile) 
{
    // Get the app's local folder to use as the destination folder.
    var localFolder = ApplicationData.current.localFolder;

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

    // Copy the file to the destination folder and rename it.
    // Replace the existing file if the file already exists.
    var NameCollisionOption = Windows.Storage.NameCollisionOption;
    return newFile.copyAsync(localFolder, renamedFileName, NameCollisionOption.replaceExisting);
});
copiedFilePromise.done(function copySuccess(copiedFile) 
{
    // File was copied to the destination folder.
}, function copyFail(failure) 
{
    // File creation or copy failed.
});

Remarks

This method uses the FailIfExists value from the CreationCollisionOption enumeration by default. That is, this method raises an exception if a file with the same name already exists in the destination folder. If you want to handle a file name collision in a different way, call the CopyAsync(IStorageFolder, String, NameCollisionOption) overloaded method.

Note

When StorageFile.CopyAsync is used to copy a file that is encrypted to a destination that is not encrypted, the call will fail with the following exception: System.UnauthorizedAccessException: Access is denied. (Excep_FromHResult 0x80070005)

See also

CopyAsync(IStorageFolder, String) CopyAsync(IStorageFolder, String) CopyAsync(IStorageFolder, String) CopyAsync(IStorageFolder, String) CopyAsync(IStorageFolder, String)

Creates a copy of the file in the specified folder and renames the copy.

public : IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder, Platform::String desiredNewName)
IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder, winrt::hstring desiredNewName) const;
public IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder, String desiredNewName)
Public Function CopyAsync(destinationFolder As IStorageFolder, desiredNewName As String) As IAsyncOperation(Of StorageFile)
Windows.Storage.StorageFile.copyAsync(destinationFolder, desiredNewName).done( /* Your success and error handlers */ );

Parameters

destinationFolder
IStorageFolder IStorageFolder IStorageFolder

The destination folder where the copy of the file is created.

desiredNewName
String String String

The new name for the copy of the file created in the destinationFolder.

Returns

When this method completes, it returns a StorageFile that represents the copy of the file created in the destinationFolder.

Note

When StorageFile.CopyAsync is used to copy a file that is encrypted to a destination that is not encrypted, the call will fail with the following exception: System.UnauthorizedAccessException: Access is denied. (Excep_FromHResult 0x80070005)

Exceptions

UnauthorizedAccessException UnauthorizedAccessException UnauthorizedAccessException

You don't have permission to copy a file into the destination folder.

See also

CopyAsync(IStorageFolder, String, NameCollisionOption) CopyAsync(IStorageFolder, String, NameCollisionOption) CopyAsync(IStorageFolder, String, NameCollisionOption) CopyAsync(IStorageFolder, String, NameCollisionOption) CopyAsync(IStorageFolder, String, NameCollisionOption)

Creates a copy of the file in the specified folder and renames the copy. This method also specifies what to do if a file with the same name already exists in the destination folder.

public : IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder, Platform::String desiredNewName, NameCollisionOption option)
IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder, winrt::hstring desiredNewName, NameCollisionOption option) const;
public IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder, String desiredNewName, NameCollisionOption option)
Public Function CopyAsync(destinationFolder As IStorageFolder, desiredNewName As String, option As NameCollisionOption) As IAsyncOperation(Of StorageFile)
Windows.Storage.StorageFile.copyAsync(destinationFolder, desiredNewName, option).done( /* Your success and error handlers */ );

Parameters

destinationFolder
IStorageFolder IStorageFolder IStorageFolder

The destination folder where the copy of the file is created.

desiredNewName
String String String

The new name for the copy of the file created in the destinationFolder.

option
NameCollisionOption NameCollisionOption NameCollisionOption

One of the enumeration values that determines how to handle the collision if a file with the specified desiredNewName already exists in the destination folder.

Returns

When this method completes, it returns a StorageFile that represents the copy of the file created in the destinationFolder.

Note

When StorageFile.CopyAsync is used to copy a file that is encrypted to a destination that is not encrypted, the call will fail with the following exception: System.UnauthorizedAccessException: Access is denied. (Excep_FromHResult 0x80070005)

Exceptions

UnauthorizedAccessException UnauthorizedAccessException UnauthorizedAccessException

You don't have permission to copy a file into the destination folder.

See also