Windows Dev Center

StorageFile.CopyAsync(IStorageFolder, String, NameCollisionOption) | copyAsync(IStorageFolder, String, NameCollisionOption) method

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.

Syntax


public:
IAsyncOperation<StorageFile>^ CopyAsync(
  IStorageFolder^ destinationFolder, 
  String^ desiredNewName, 
  NameCollisionOption option
)

Parameters

destinationFolder

Type: IStorageFolder

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

desiredNewName

Type: String [JavaScript] | System.String [.NET] | Platform::String [C++]

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

option

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

Return value

Type: IAsyncOperation<StorageFile>

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

Exceptions

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


	//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 exisitng 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
	});


Requirements (device family)

Device family

Universal

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0

Namespace

Windows.Storage
Windows::Storage [C++]

Metadata

Windows.Foundation.UniversalApiContract.winmd

Requirements (operating system)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8

Namespace

Windows.Storage
Windows::Storage [C++]

Metadata

Windows.winmd

See also

StorageFile
CopyAsync(IStorageFolder)
CopyAsync(IStorageFolder, String)

 

 

Show:
© 2015 Microsoft