-api-id | -api-type |
---|---|
M:Windows.Storage.StorageFolder.CreateFolderAsync(System.String) |
winrt method |
Creates a new subfolder with the specified name in the current folder.
The name of the new subfolder to create in the current folder.
When this method completes, it returns a StorageFolder that represents the new subfolder.
The folder name contains invalid characters, or the format of the folder name is incorrect. Check the value of desiredName.
You don't have permission to create a subfolder in the current folder.
This method uses the FailIfExists value from the CreationCollisionOption enumeration by default. That is, this method raises an exception if a subfolder with the same name already exists in the current folder. If you want to handle a folder name collision in a different way, call the CreateFolderAsync(String, CreationCollisionOption) method.
If you try to create a subfolder 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 CreateFolderAsync method may fail.
The following example shows how to create a new StorageFolder in the current folder by calling the CreateFolderAsync(String, CreationCollisionOption) overloaded method. This example explicitly specifies a value for options that causes the operation to fail if a folder 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 subfolder in the current folder.
// Raise an exception if the folder already exists.
string desiredName = "Subfolder";
StorageFolder newFolder =
await localFolder.CreateFolderAsync(desiredName, CreationCollisionOption.FailIfExists);
IAsyncAction MainPage::ExampleCoroutineAsync()
{
// Get the app's local folder.
Windows::Storage::StorageFolder localFolder{ Windows::Storage::ApplicationData::Current().LocalFolder() };
// Create a new subfolder in the current folder.
// Throw an exception if it already exists.
std::wstring desiredName{ L"Subfolder" };
Windows::Storage::StorageFolder folder{ co_await localFolder.CreateFolderAsync(desiredName, Windows::Storage::CreationCollisionOption::FailIfExists) };
// Do something with folder.
}
//Get app's local folder
StorageFolder^ localFolder = Windows::Storage::ApplicationData::Current->LocalFolder;
// Create a new subfolder in the current folder
// Raise an exception if it already exists
String^ desiredName = "Subfolder";
create_task(localFolder->CreateFolderAsync(desiredName,Windows::Storage::CreationCollisionOption::FailIfExists)).then([](StorageFolder^ folder)
{
//Do something with folder
});