StorageFolder.CreateFileAsync Metode

Definisi

Overload

CreateFileAsync(String)

Membuat file baru dengan nama yang ditentukan di folder saat ini.

CreateFileAsync(String, CreationCollisionOption)

Membuat file baru di folder saat ini. Metode ini juga menentukan apa yang harus dilakukan jika file dengan nama yang sama sudah ada di folder saat ini.

CreateFileAsync(String)

Membuat file baru dengan nama yang ditentukan di folder saat ini.

public:
 virtual IAsyncOperation<StorageFile ^> ^ CreateFileAsync(Platform::String ^ desiredName) = CreateFileAsync;
/// [Windows.Foundation.Metadata.Overload("CreateFileAsyncOverloadDefaultOptions")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<StorageFile> CreateFileAsync(winrt::hstring const& desiredName);
[Windows.Foundation.Metadata.Overload("CreateFileAsyncOverloadDefaultOptions")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<StorageFile> CreateFileAsync(string desiredName);
function createFileAsync(desiredName)
Public Function CreateFileAsync (desiredName As String) As IAsyncOperation(Of StorageFile)

Parameter

desiredName
String

Platform::String

winrt::hstring

Nama file baru yang akan dibuat di folder saat ini.

Mengembalikan

Ketika metode ini selesai, metode ini mengembalikan StorageFile yang mewakili file baru.

Penerapan

M:Windows.Storage.IStorageFolder.CreateFileAsync(System.String) M:Windows.Storage.IStorageFolder.CreateFileAsync(Platform::String) M:Windows.Storage.IStorageFolder.CreateFileAsync(winrt::hstring)
Atribut

Pengecualian

Nama file berisi karakter yang tidak valid, atau format nama file salah. Periksa nilai desiredName.

Anda tidak memiliki izin untuk membuat file di folder saat ini.

Contoh

Contoh berikut menunjukkan cara membuat file baru di folder saat ini dengan memanggil metode kelebihan beban CreateFileAsync (String, CreationCollisionOption). Contoh ini secara eksplisit menentukan nilai untuk opsi yang menyebabkan operasi gagal jika file dengan desiredName yang ditentukan sudah ada di folder saat ini.

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);
IAsyncAction MainPage::ExampleCoroutineAsync()
{
    // Gets the app's local folder.
    Windows::Storage::StorageFolder localFolder{ Windows::Storage::ApplicationData::Current().LocalFolder() };

    // Create a new file in the current folder, and throw an exception if the file already exists.
    std::wstring desiredName{ L"test.txt" };
    StorageFile newFile{ co_await localFolder.CreateFileAsync(desiredName, Windows::Storage::CreationCollisionOption::FailIfExists) };
    // Do something with the new file.
}
//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.
 });

Keterangan

Metode ini menggunakan nilai FailIfExists dari enumerasi CreationCollisionOption secara default. Artinya, metode ini menimbulkan pengecualian jika file dengan nama yang sama sudah ada di folder saat ini. Jika Anda ingin menangani tabrakan nama file dengan cara yang berbeda, panggil metode CreateFileAsync(String, CreationCollisionOption).

Jika Anda mencoba membuat file di folder virtual seperti pustaka, atau folder yang mewakili kontainer untuk sekelompok file (misalnya, nilai yang dikembalikan dari beberapa kelebihan beban metode GetFoldersAsync ), metode CreateFileAsync mungkin gagal.

Lihat juga

Berlaku untuk

CreateFileAsync(String, CreationCollisionOption)

Membuat file baru di folder saat ini. Metode ini juga menentukan apa yang harus dilakukan jika file dengan nama yang sama sudah ada di folder saat ini.

public:
 virtual IAsyncOperation<StorageFile ^> ^ CreateFileAsync(Platform::String ^ desiredName, CreationCollisionOption options) = CreateFileAsync;
/// [Windows.Foundation.Metadata.Overload("CreateFileAsync")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<StorageFile> CreateFileAsync(winrt::hstring const& desiredName, CreationCollisionOption const& options);
[Windows.Foundation.Metadata.Overload("CreateFileAsync")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<StorageFile> CreateFileAsync(string desiredName, CreationCollisionOption options);
function createFileAsync(desiredName, options)
Public Function CreateFileAsync (desiredName As String, options As CreationCollisionOption) As IAsyncOperation(Of StorageFile)

Parameter

desiredName
String

Platform::String

winrt::hstring

Nama file baru yang akan dibuat di folder saat ini.

options
CreationCollisionOption

Salah satu nilai enumerasi yang menentukan cara menangani tabrakan jika file dengan desiredName yang ditentukan sudah ada di folder saat ini.

Mengembalikan

Ketika metode ini selesai, metode ini mengembalikan StorageFile yang mewakili file baru.

Penerapan

M:Windows.Storage.IStorageFolder.CreateFileAsync(System.String,Windows.Storage.CreationCollisionOption) M:Windows.Storage.IStorageFolder.CreateFileAsync(Platform::String,Windows.Storage.CreationCollisionOption) M:Windows.Storage.IStorageFolder.CreateFileAsync(winrt::hstring,Windows.Storage.CreationCollisionOption)
Atribut

Pengecualian

Anda menentukan CreationCollisionOption.FailIfExists dan file dengan desiredName yang ditentukan sudah ada di folder saat ini.

Format nama file salah. Periksa nilai desiredName.

Anda tidak memiliki izin untuk membuat file di folder saat ini.

Lihat juga

Berlaku untuk