CreateFileAsync(String, CreationCollisionOption) | createFileAsync(String, CreationCollisionOption) method
Collapse the table of content
Expand the table of content

StorageFolder.CreateFileAsync(String, CreationCollisionOption) | createFileAsync(String, CreationCollisionOption) method

Creates a new file in the current folder. This method also specifies what to do if a file with the same name already exists in the current folder.

Syntax


public:
IAsyncOperation<StorageFile>^ CreateFileAsync(
  String^ desiredName, 
  CreationCollisionOption options
)

Parameters

desiredName

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

The name of the new file to create in the current folder.

options

Type: CreationCollisionOption

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

Return value

Type: IAsyncOperation<StorageFile>

When this method completes, it returns a StorageFile that represents the new file.

Exceptions

ExceptionCondition
System.Exception

You specified CreationCollisionOption.FailIfExists and a file with the specified desiredName already exists in the current folder.

System.IO.FileNotFoundException

The format of the filename is incorrect. Check the value of desiredName.

System.UnauthorizedAccessException

You don't have permission to create a file in the current folder.

Remarks

If a file with the specified desiredName already exists in the current folder, the specified CreationCollisionOption determines how to handle the collision.

If you try to create a file in a virtual folder like a library, or a folder that represents a container for a group of files (for example, the return value from some overloads of the GetFoldersAsync method), the CreateFileAsync method may fail.

Examples

The following example shows how to create a new file in the current folder by calling the CreateFileAsync (String, CreationCollisionOption) method. This example explicitly specifies a value for options that causes the operation to fail if a file with the specified desiredName already exists in the current folder.


 //Gets the app's local folder
 StorageFolder^ localFolder = ApplicationData::Current->LocalFolder;

 //Create a new file in the current folder
 // Raise an exception if the file already exists
 String^ desiredName = "test.txt";
 auto createFileTask = create_task(localFolder->CreateFileAsync(desiredName, Windows::Storage::CreationCollisionOption::FailIfExists));
 createFileTask.then([](StorageFile^ newFile){
  //Do something with the new file.

 });


Requirements (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0

Namespace

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

Metadata

Windows.Foundation.UniversalApiContract.winmd

Requirements (Windows 8.x and Windows Phone 8.x)

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

StorageFolder
CreateFileAsync(String)

 

 

Show:
© 2016 Microsoft