Share via


FileIO 類別

定義

提供協助程式方法來讀取和寫入 IStorageFile類型物件所表示的檔案。

public ref class FileIO abstract sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class FileIO final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public static class FileIO
Public Class FileIO
繼承
Object Platform::Object IInspectable FileIO
屬性

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

檔案 存取範例 示範如何使用 WriteTextAsync (檔案、內容) 將文字寫入檔案。

try
{
    if (file != null)
    {
        await FileIO.WriteTextAsync(file, "Swift as a shadow");
    }
}
catch (FileNotFoundException)
{
    // For example, handle file not found
}
try
{
    if (file)
    {
        co_await FileIO::WriteTextAsync(file, L"Swift as a shadow");
    }
}
catch (hresult_error const& ex)
{
    if (ex.code() == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
    {
        // For example, handle file not found
    }
}

在此範例中, file 是區域變數,其中包含代表要寫入之檔案的 StorageFile

雖然 WriteTextAsync 方法沒有傳回值,但您仍然可以在將文字寫入檔案之後執行其他工作,如範例所示。檔案 存取範例 也會示範如何使用 ReadTextAsync (檔案) 從檔案讀取文字。

try
{
    if (file != null)
    {
        string fileContent = await FileIO.ReadTextAsync(file);
    }
}
catch (FileNotFoundException)
{
    // For example, handle file not found
}
try
{
    if (file)
    {
        hstring fileContent = co_await FileIO::ReadTextAsync(file);
    }
}
catch (hresult_error const& ex)
{
    if (ex.code() == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND))
    {
        // For example, handle file not found
    }
}

在此範例中, file 是區域變數,其中包含代表要讀取之檔案的 StorageFile

ReadTextAsync完成之後, fileContent 變數會以文字字串的形式取得檔案的內容。 然後,您可以視需要處理內容。

備註

這個類別是靜態的,無法具現化。 請改為直接呼叫 方法。

若要深入瞭解您的應用程式可以存取的位置,請參閱 檔案存取權限

若要瞭解如何讀取和寫入檔案,請參閱 建立、寫入和讀取檔案

方法

AppendLinesAsync(IStorageFile, IIterable<String>)

將文字行附加至指定的檔案。

AppendLinesAsync(IStorageFile, IIterable<String>, UnicodeEncoding)

使用指定的字元編碼,將文字行附加至指定的檔案。

AppendTextAsync(IStorageFile, String)

將文字附加至指定的檔案。

AppendTextAsync(IStorageFile, String, UnicodeEncoding)

使用指定的字元編碼,將文字附加至指定的檔案。

ReadBufferAsync(IStorageFile)

讀取指定檔案的內容,並傳回緩衝區。

ReadLinesAsync(IStorageFile)

讀取指定檔案的內容,並傳回文字行。

ReadLinesAsync(IStorageFile, UnicodeEncoding)

使用指定的字元編碼讀取指定檔案的內容,並傳回文字行。

ReadTextAsync(IStorageFile)

讀取指定檔案的內容並傳回文字。

ReadTextAsync(IStorageFile, UnicodeEncoding)

使用指定的字元編碼讀取指定檔案的內容,並傳回文字。

WriteBufferAsync(IStorageFile, IBuffer)

將資料從緩衝區寫入指定的檔案。

WriteBytesAsync(IStorageFile, Byte[])

將資料位元組陣列寫入指定的檔案。

WriteLinesAsync(IStorageFile, IIterable<String>)

將文字行寫入指定的檔案。

WriteLinesAsync(IStorageFile, IIterable<String>, UnicodeEncoding)

使用指定的字元編碼,將文字行寫入指定的檔案。

WriteTextAsync(IStorageFile, String)

將文字寫入指定的檔案。

WriteTextAsync(IStorageFile, String, UnicodeEncoding)

使用指定的字元編碼,將文字寫入指定的檔案。

適用於