StorageFolder.CreateFileAsync 方法

定義

多載

CreateFileAsync(String)

在目前資料夾中建立具有指定名稱的新檔案。

CreateFileAsync(String, CreationCollisionOption)

在目前資料夾中建立新的檔案。 這個方法也會指定如果具有相同名稱的檔案已存在於目前資料夾中,該怎麼辦。

CreateFileAsync(String)

在目前資料夾中建立具有指定名稱的新檔案。

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)

參數

desiredName
String

Platform::String

winrt::hstring

要建立于目前資料夾中之新檔案的名稱。

傳回

此方法完成時,它會傳回代表新檔案的 StorageFile

實作

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

例外狀況

檔案名包含不正確字元,或檔案名的格式不正確。 檢查 desiredName的值。

您沒有在目前資料夾中建立檔案的許可權。

範例

下列範例示範如何藉由呼叫 CreateFileAsync (String、CreationCollisionOption) 多載方法,在目前資料夾中建立新檔案。 如果目前資料夾中已有具有指定desiredName的檔案,這個範例會明確指定選項的值,使作業失敗。

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.
 });

備註

此方法預設會使用CreationCollisionOption列舉中的FailIfExists值。 也就是說,如果同名的檔案已存在於目前資料夾中,這個方法就會引發例外狀況。 如果您想要以不同方式處理檔案名衝突,請呼叫 CreateFileAsync (String、CreationCollisionOption) 方法。

如果您嘗試在虛擬資料夾中建立檔案,例如程式庫,或代表一組 (檔案之容器的資料夾,例如, GetFoldersAsync 方法的某些多載傳回值) , CreateFileAsync 方法可能會失敗。

另請參閱

適用於

CreateFileAsync(String, CreationCollisionOption)

在目前資料夾中建立新的檔案。 這個方法也會指定如果具有相同名稱的檔案已存在於目前資料夾中,該怎麼辦。

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)

參數

desiredName
String

Platform::String

winrt::hstring

要建立于目前資料夾中之新檔案的名稱。

options
CreationCollisionOption

其中一個列舉值,決定當具有指定 desiredName 的檔案已存在於目前資料夾中時,如何處理衝突。

傳回

此方法完成時,它會傳回代表新檔案的 StorageFile

實作

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)
屬性

例外狀況

您指定的 CreationCollisionOption.FailIfExists 和具有指定 desiredName 的檔案已存在於目前資料夾中。

檔案名的格式不正確。 檢查 desiredName的值。

您沒有在目前資料夾中建立檔案的許可權。

另請參閱

適用於