StorageFolder.CreateFileAsync StorageFolder.CreateFileAsync StorageFolder.CreateFileAsync StorageFolder.CreateFileAsync StorageFolder.CreateFileAsync Method

Definition

Overloads

CreateFileAsync(String) CreateFileAsync(String) CreateFileAsync(String) CreateFileAsync(String) CreateFileAsync(String)

Creates a new file with the specified name in the current folder.

CreateFileAsync(String, CreationCollisionOption) CreateFileAsync(String, CreationCollisionOption) CreateFileAsync(String, CreationCollisionOption) CreateFileAsync(String, CreationCollisionOption) CreateFileAsync(String, CreationCollisionOption)

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.

CreateFileAsync(String) CreateFileAsync(String) CreateFileAsync(String) CreateFileAsync(String) CreateFileAsync(String)

Creates a new file with the specified name in the current folder.

public : IAsyncOperation<StorageFile> CreateFileAsync(Platform::String desiredName)
IAsyncOperation<StorageFile> CreateFileAsync(winrt::hstring desiredName) const;
public IAsyncOperation<StorageFile> CreateFileAsync(String desiredName)
Public Function CreateFileAsync(desiredName As String) As IAsyncOperation(Of StorageFile)
Windows.Storage.StorageFolder.createFileAsync(desiredName).done( /* Your success and error handlers */ );
Parameters
desiredName
String String String

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

Returns

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

Exceptions
FileNotFoundException FileNotFoundException FileNotFoundException

The file name contains invalid characters, or the format of the filename is incorrect. Check the value of desiredName.

UnauthorizedAccessException UnauthorizedAccessException UnauthorizedAccessException

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

Examples

The following example shows how to create a new file in the current folder by calling the CreateFileAsync (String, CreationCollisionOption) overloaded 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.

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

// Get the app's local folder.
StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

// Create a new file in the current folder.
// Raise an exception if the file already exists.
string desiredName = "test.txt";
StorageFile newFile = await localFolder.CreateFileAsync(desiredName, CreationCollisionOption.FailIfExists);
// Get the app's local folder.
var localFolder = Windows.Storage.ApplicationData.current.localFolder;

// Create a new file in the current folder.
var desiredName = "test.txt";
var CreationCollisionOption = Windows.Storage.CreationCollisionOption;
var newFilePromise =
        localFolder.createFileAsync(desiredName, CreationCollisionOption.failIfExists);
newFilePromise.done(function createFileSuccess(newFile) 
{
    // CreateFile completed successfully.
}, function createFileFail(failure) 
{
    // CreateFile can fail if the file already exists.
});
//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.
 });

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 current folder. If you want to handle a file name collision in a different way, call the CreateFileAsync(String, CreationCollisionOption) method.

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.

See Also

CreateFileAsync(String, CreationCollisionOption) CreateFileAsync(String, CreationCollisionOption) CreateFileAsync(String, CreationCollisionOption) CreateFileAsync(String, CreationCollisionOption) CreateFileAsync(String, CreationCollisionOption)

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.

public : IAsyncOperation<StorageFile> CreateFileAsync(Platform::String desiredName, CreationCollisionOption options)
IAsyncOperation<StorageFile> CreateFileAsync(winrt::hstring desiredName, CreationCollisionOption options) const;
public IAsyncOperation<StorageFile> CreateFileAsync(String desiredName, CreationCollisionOption options)
Public Function CreateFileAsync(desiredName As String, options As CreationCollisionOption) As IAsyncOperation(Of StorageFile)
Windows.Storage.StorageFolder.createFileAsync(desiredName, options).done( /* Your success and error handlers */ );
Parameters
desiredName
String String String

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

options
CreationCollisionOption CreationCollisionOption 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.

Returns

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

Exceptions
Exception Exception Exception

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

FileNotFoundException FileNotFoundException FileNotFoundException

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

UnauthorizedAccessException UnauthorizedAccessException UnauthorizedAccessException

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

See Also