PathIO 類別

定義

提供使用絕對路徑或統一資源識別項 (URI) 檔案讀取和寫入檔案的協助程式方法。

public ref class PathIO 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 PathIO 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 PathIO
Public Class PathIO
繼承
Object Platform::Object IInspectable PathIO
屬性

Windows 需求

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

範例

您可以使用 WriteTextAsync (filePathOrURI,內容) 將文字寫入檔案。 在此範例中, filePathOrURI 是包含 URI (的區域變數,例如應用程式 URI 「ms-appdata://sampleFile.dat」) 或檔案系統路徑 (,例如 C:\examplepath\sampleFile.dat) ,以供檔案讀取。 雖然 WriteTextAsync 方法沒有傳回值,但在文字寫入檔案之後,您仍然可以執行其他工作,如範例所示。

try
{
    if (file != null)
    {
        await PathIO.WriteTextAsync(filePathOrURI, "Swift as a shadow");
    }
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
    // For example, handle file not found
}

您可以使用 ReadTextAsync (filePathOrURI) 從檔案讀取文字。 ReadTextAsync完成後, fileContent 變數會取得檔案的內容做為文字字串。 然後,您可以視需要處理內容。

try
{
    if (file != null)
    {
        string fileContent = await PathIO.ReadTextAsync(filePathOrURI);
    }
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
    // For example, handle file not found
}

如需詳細資訊,請參閱 檔案存取範例FileIO

備註

若要使用路徑或統一資源識別項來讀取或寫入檔案, (URI) ,您必須具有存取檔案位置的許可權。 若要深入瞭解您的應用程式有權存取的位置,請參閱 檔案存取權限

您可以指定應用程式統一資源識別項 (URI) ,以讀取或寫入應用程式特定位置中的檔案,例如您的應用程式資料檔案夾 (「ms-appdata://」) 。 若要深入瞭解應用程式統一資源識別項 (URI) ,請參閱 如何參考內容

方法

AppendLinesAsync(String, IIterable<String>)

在指定的路徑或統一資源識別項 (URI) ,將文字行附加至檔案。

AppendLinesAsync(String, IIterable<String>, UnicodeEncoding)

使用指定的字元編碼,將文字行附加到檔案中指定的路徑或統一資源識別項 (URI) 。

AppendTextAsync(String, String)

將文字附加至位於指定路徑或統一資源識別項 (URI) 的檔案。

AppendTextAsync(String, String, UnicodeEncoding)

使用指定的字元編碼,將文字附加至位於指定路徑或統一資源識別項的檔案 (URI) 。

ReadBufferAsync(String)

在指定的路徑或統一資源識別項 (URI 讀取檔案的內容) ,並傳回緩衝區。

ReadLinesAsync(String)

讀取位於指定路徑或統一資源識別項 (URI 的檔案內容,) 並傳回文字行。

ReadLinesAsync(String, UnicodeEncoding)

使用指定的字元編碼,讀取位於指定路徑或統一資源識別元 (URI) 檔案的內容,並傳回文字行。

ReadTextAsync(String)

在指定的路徑或統一資源識別項 (URI 讀取檔案的內容,) 並傳回文字。

ReadTextAsync(String, UnicodeEncoding)

使用指定的字元編碼方式,讀取位於指定路徑或統一資源識別項 (URI) 檔案的內容,並傳回文字。

WriteBufferAsync(String, IBuffer)

將資料從緩衝區寫入至位於指定路徑或統一資源識別項的檔案, (URI) 。

WriteBytesAsync(String, Byte[])

在指定的路徑或統一資源識別項 (URI) ,將資料位元組寫入檔案。

WriteLinesAsync(String, IIterable<String>)

將文字行寫入檔案中指定的路徑或統一資源識別項 (URI) 。

WriteLinesAsync(String, IIterable<String>, UnicodeEncoding)

使用指定的字元編碼,在指定的路徑或統一資源識別項 (URI) ,將文字行寫入檔案。

WriteTextAsync(String, String)

在指定的路徑或統一資源識別項 (URI) ,將文字寫入檔案。

WriteTextAsync(String, String, UnicodeEncoding)

使用指定的字元編碼,在指定的路徑或統一資源識別項 (URI) ,將文字寫入檔案。

適用於

另請參閱