PathIO PathIO PathIO PathIO Class

Provides helper methods for reading and writing a file using the absolute path or Uniform Resource Identifier (URI) of the file.

Syntax

Declaration

public static class PathIOpublic static class PathIOPublic Static Class PathIOpublic static class PathIO

Remarks

To read or write to a file using a path or Uniform Resource Identifier (URI), you must have permission to access the file's location. To learn more about what locations your app has permission to access, see File access permissions.

You can specify an app Uniform Resource Identifier (URI) to read or write files in app-specific locations like your app data folder ("ms-appdata://"). To learn more about app Uniform Resource Identifier (URI), see How to reference content.

Examples

You can use WriteTextAsync(String, String) to write text to a file. In the example, filePathOrURI is a local variable that contains a URI (like an app URI "ms-appdata://sampleFile.dat") or a file system path (like C:\examplepath\sampleFile.dat) for the file to read. Although the WriteTextAsync(String, String) methods don't have a return value, you can still perform additional tasks after the text is written to the file, as the example shows.


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
}

if (file !== null) {
    Windows.Storage.PathIO.writeTextAsync(filePathOrURI, "Swift as a shadow").done(function () {
        // Perform additional tasks after file is written
    },
    // Handle errors with an error function
    function (error) {
        // Handle errors encountered during write
    });
}

You can use ReadTextAsync(String) to read text from a file. After ReadTextAsync(String) completes, the fileContent variable gets the contents of the file as a text string. You can then process the contents as appropriate.

if (file !== null) {
    Windows.Storage.PathIO.readTextAsync(filePathOrURI).done(function (fileContent) {
        // Process content read from the file
    },
    // Handle errors with an error function
    function (error) {
        // Handle errors encountered during read
    });
}
try
{
    if (file != null)
    {
        string fileContent = await PathIO.ReadTextAsync(filePathOrURI);
    }
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
    // For example, handle file not found
}

For more, related examples, see File Access sample and FileIO.

Methods summary

Appends lines of text to the file at the specified path or Uniform Resource Identifier (URI).

Appends lines of text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.

Appends text to the file at the specified path or Uniform Resource Identifier (URI).

Appends text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.

Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) and returns a buffer.

Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) and returns lines of text.

Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding and returns lines of text.

Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) and returns text.

Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding and returns text.

Writes data from a buffer to the file at the specified path or Uniform Resource Identifier (URI).

Writes a single byte of data to the file at the specified path or Uniform Resource Identifier (URI).

Writes lines of text to the file at the specified path or Uniform Resource Identifier (URI).

Writes lines of text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.

Writes text to the file at the specified path or Uniform Resource Identifier (URI).

Writes text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.

Methods

  • AppendLinesAsync(String, IIterable<String>)
    AppendLinesAsync(String, IIterable<String>)
    AppendLinesAsync(String, IIterable<String>)
    AppendLinesAsync(String, IIterable<String>)

    Appends lines of text to the file at the specified path or Uniform Resource Identifier (URI).

    public static IAsyncAction AppendLinesAsync(String absolutePath, IIterable<String> lines)public static IAsyncAction AppendLinesAsync(String absolutePath, IIterable<String> lines)Public Static Function AppendLinesAsync(absolutePath As String, lines As IIterable<String>) As IAsyncActionpublic static IAsyncAction AppendLinesAsync(String absolutePath, IIterable<String> lines)

    Parameters

    • absolutePath
      System.String
      System.String
      System.String
      System.String

      The path or Uniform Resource Identifier (URI) of the file that the lines are appended to.

    • lines

      The list of text strings to append as lines.

    Returns

  • AppendLinesAsync(String, IIterable<String>, UnicodeEncoding)
    AppendLinesAsync(String, IIterable<String>, UnicodeEncoding)
    AppendLinesAsync(String, IIterable<String>, UnicodeEncoding)
    AppendLinesAsync(String, IIterable<String>, UnicodeEncoding)

    Appends lines of text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.

    public static IAsyncAction AppendLinesAsync(String absolutePath, IIterable<String> lines, UnicodeEncoding encoding)public static IAsyncAction AppendLinesAsync(String absolutePath, IIterable<String> lines, UnicodeEncoding encoding)Public Static Function AppendLinesAsync(absolutePath As String, lines As IIterable<String>, encoding As UnicodeEncoding) As IAsyncActionpublic static IAsyncAction AppendLinesAsync(String absolutePath, IIterable<String> lines, UnicodeEncoding encoding)

    Parameters

    Returns

  • AppendTextAsync(String, String)
    AppendTextAsync(String, String)
    AppendTextAsync(String, String)
    AppendTextAsync(String, String)

    Appends text to the file at the specified path or Uniform Resource Identifier (URI).

    public static IAsyncAction AppendTextAsync(String absolutePath, String contents)public static IAsyncAction AppendTextAsync(String absolutePath, String contents)Public Static Function AppendTextAsync(absolutePath As String, contents As String) As IAsyncActionpublic static IAsyncAction AppendTextAsync(String absolutePath, String contents)

    Parameters

    • absolutePath
      System.String
      System.String
      System.String
      System.String

      The path of the file that the text is appended to.

    • contents
      System.String
      System.String
      System.String
      System.String

      The text to append.

    Returns

  • AppendTextAsync(String, String, UnicodeEncoding)
    AppendTextAsync(String, String, UnicodeEncoding)
    AppendTextAsync(String, String, UnicodeEncoding)
    AppendTextAsync(String, String, UnicodeEncoding)

    Appends text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.

    public static IAsyncAction AppendTextAsync(String absolutePath, String contents, UnicodeEncoding encoding)public static IAsyncAction AppendTextAsync(String absolutePath, String contents, UnicodeEncoding encoding)Public Static Function AppendTextAsync(absolutePath As String, contents As String, encoding As UnicodeEncoding) As IAsyncActionpublic static IAsyncAction AppendTextAsync(String absolutePath, String contents, UnicodeEncoding encoding)

    Parameters

    • absolutePath
      System.String
      System.String
      System.String
      System.String

      The path of the file that the text is appended to.

    • contents
      System.String
      System.String
      System.String
      System.String

      The text to append.

    • encoding

      The character encoding of the file.

    Returns

  • ReadBufferAsync(String)
    ReadBufferAsync(String)
    ReadBufferAsync(String)
    ReadBufferAsync(String)

    Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) and returns a buffer.

    public static IAsyncOperation<IBuffer> ReadBufferAsync(String absolutePath)public static IAsyncOperation<IBuffer> ReadBufferAsync(String absolutePath)Public Static Function ReadBufferAsync(absolutePath As String) As IAsyncOperation( Of IBuffer )public static IAsyncOperation<IBuffer> ReadBufferAsync(String absolutePath)

    Parameters

    • absolutePath
      System.String
      System.String
      System.String
      System.String

      The path of the file to read.

    Returns

    • When this method completes, it returns an object (type IBuffer ) that represents the contents of the file.

  • ReadLinesAsync(String)
    ReadLinesAsync(String)
    ReadLinesAsync(String)
    ReadLinesAsync(String)

    Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) and returns lines of text.

    public static IAsyncOperation<IVector<string>> ReadLinesAsync(String absolutePath)public static IAsyncOperation<IVector<string>> ReadLinesAsync(String absolutePath)Public Static Function ReadLinesAsync(absolutePath As String) As IAsyncOperation( Of IVectorstring )public static IAsyncOperation<IVector<string>> ReadLinesAsync(String absolutePath)

    Parameters

    • absolutePath
      System.String
      System.String
      System.String
      System.String

      The path of the file to read.

    Returns

    • When this method completes successfully, it returns the contents of the file as a list (type IVector<T> ) of lines of text. Each line of text in the list is represented by a String object.

  • ReadLinesAsync(String, UnicodeEncoding)
    ReadLinesAsync(String, UnicodeEncoding)
    ReadLinesAsync(String, UnicodeEncoding)
    ReadLinesAsync(String, UnicodeEncoding)

    Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding and returns lines of text.

    public static IAsyncOperation<IVector<string>> ReadLinesAsync(String absolutePath, UnicodeEncoding encoding)public static IAsyncOperation<IVector<string>> ReadLinesAsync(String absolutePath, UnicodeEncoding encoding)Public Static Function ReadLinesAsync(absolutePath As String, encoding As UnicodeEncoding) As IAsyncOperation( Of IVectorstring )public static IAsyncOperation<IVector<string>> ReadLinesAsync(String absolutePath, UnicodeEncoding encoding)

    Parameters

    Returns

    • When this method completes successfully, it returns the contents of the file as a list (type IVector<T> ) of lines of text. Each line of text in the list is represented by a String object.

  • ReadTextAsync(String)
    ReadTextAsync(String)
    ReadTextAsync(String)
    ReadTextAsync(String)

    Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) and returns text.

    public static IAsyncOperation<string> ReadTextAsync(String absolutePath)public static IAsyncOperation<string> ReadTextAsync(String absolutePath)Public Static Function ReadTextAsync(absolutePath As String) As IAsyncOperation( Of string )public static IAsyncOperation<string> ReadTextAsync(String absolutePath)

    Parameters

    • absolutePath
      System.String
      System.String
      System.String
      System.String

      The path of the file to read.

    Returns

    • When this method completes successfully, it returns the contents of the file as a text string.

    Remarks

    Examples

    This example shows you how to use ReadTextAsync(String) to read text from the file at the path in absoluteFilePath like this:

    Windows.Storage.PathIO.readTextAsync(absoluteFilePath).then(function (contents) {
        // Add code to process the text read from the file
    });
    

    You can use then or done to declare a function to capture and process the text that was read from the file. After the ReadTextAsync(String) method completes, the text will be passed to this function as a string object (contents in the example).

  • ReadTextAsync(String, UnicodeEncoding)
    ReadTextAsync(String, UnicodeEncoding)
    ReadTextAsync(String, UnicodeEncoding)
    ReadTextAsync(String, UnicodeEncoding)

    Reads the contents of the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding and returns text.

    public static IAsyncOperation<string> ReadTextAsync(String absolutePath, UnicodeEncoding encoding)public static IAsyncOperation<string> ReadTextAsync(String absolutePath, UnicodeEncoding encoding)Public Static Function ReadTextAsync(absolutePath As String, encoding As UnicodeEncoding) As IAsyncOperation( Of string )public static IAsyncOperation<string> ReadTextAsync(String absolutePath, UnicodeEncoding encoding)

    Parameters

    Returns

    • When this method completes successfully, it returns the contents of the file as a text string.

  • WriteBufferAsync(String, IBuffer)
    WriteBufferAsync(String, IBuffer)
    WriteBufferAsync(String, IBuffer)
    WriteBufferAsync(String, IBuffer)

    Writes data from a buffer to the file at the specified path or Uniform Resource Identifier (URI).

    public static IAsyncAction WriteBufferAsync(String absolutePath, IBuffer buffer)public static IAsyncAction WriteBufferAsync(String absolutePath, IBuffer buffer)Public Static Function WriteBufferAsync(absolutePath As String, buffer As IBuffer) As IAsyncActionpublic static IAsyncAction WriteBufferAsync(String absolutePath, IBuffer buffer)

    Parameters

    • absolutePath
      System.String
      System.String
      System.String
      System.String

      The path of the file that the data is written to.

    • buffer

      The buffer that contains the data to write.

    Returns

  • WriteBytesAsync(String, Byte[])
    WriteBytesAsync(String, Byte[])
    WriteBytesAsync(String, Byte[])
    WriteBytesAsync(String, Byte[])

    Writes a single byte of data to the file at the specified path or Uniform Resource Identifier (URI).

    public static IAsyncAction WriteBytesAsync(String absolutePath, Byte[] buffer)public static IAsyncAction WriteBytesAsync(String absolutePath, Byte[] buffer)Public Static Function WriteBytesAsync(absolutePath As String, buffer As Byte[]) As IAsyncActionpublic static IAsyncAction WriteBytesAsync(String absolutePath, Byte[] buffer)

    Parameters

    • absolutePath
      System.String
      System.String
      System.String
      System.String

      The path of the file that the byte is written to.

    • buffer
      System.Byte[]
      System.Byte[]
      System.Byte[]
      System.Byte[]

      An array of bytes to write.

    Returns

  • WriteLinesAsync(String, IIterable<String>)
    WriteLinesAsync(String, IIterable<String>)
    WriteLinesAsync(String, IIterable<String>)
    WriteLinesAsync(String, IIterable<String>)

    Writes lines of text to the file at the specified path or Uniform Resource Identifier (URI).

    public static IAsyncAction WriteLinesAsync(String absolutePath, IIterable<String> lines)public static IAsyncAction WriteLinesAsync(String absolutePath, IIterable<String> lines)Public Static Function WriteLinesAsync(absolutePath As String, lines As IIterable<String>) As IAsyncActionpublic static IAsyncAction WriteLinesAsync(String absolutePath, IIterable<String> lines)

    Parameters

    • absolutePath
      System.String
      System.String
      System.String
      System.String

      The path of the file that the lines are written to.

    • lines

      The list of text strings to append as lines.

    Returns

    Remarks

    This method attempts to automatically detect the encoding of a file based on the presence of byte order marks. If an encoding cannot be detected, the encoding specified by the caller in the method overload is used.

  • WriteLinesAsync(String, IIterable<String>, UnicodeEncoding)
    WriteLinesAsync(String, IIterable<String>, UnicodeEncoding)
    WriteLinesAsync(String, IIterable<String>, UnicodeEncoding)
    WriteLinesAsync(String, IIterable<String>, UnicodeEncoding)

    Writes lines of text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.

    public static IAsyncAction WriteLinesAsync(String absolutePath, IIterable<String> lines, UnicodeEncoding encoding)public static IAsyncAction WriteLinesAsync(String absolutePath, IIterable<String> lines, UnicodeEncoding encoding)Public Static Function WriteLinesAsync(absolutePath As String, lines As IIterable<String>, encoding As UnicodeEncoding) As IAsyncActionpublic static IAsyncAction WriteLinesAsync(String absolutePath, IIterable<String> lines, UnicodeEncoding encoding)

    Parameters

    Returns

  • WriteTextAsync(String, String)
    WriteTextAsync(String, String)
    WriteTextAsync(String, String)
    WriteTextAsync(String, String)

    Writes text to the file at the specified path or Uniform Resource Identifier (URI).

    public static IAsyncAction WriteTextAsync(String absolutePath, String contents)public static IAsyncAction WriteTextAsync(String absolutePath, String contents)Public Static Function WriteTextAsync(absolutePath As String, contents As String) As IAsyncActionpublic static IAsyncAction WriteTextAsync(String absolutePath, String contents)

    Parameters

    • absolutePath
      System.String
      System.String
      System.String
      System.String

      The path of the file that the text is written to.

    • contents
      System.String
      System.String
      System.String
      System.String

      The text to write.

    Returns

    Remarks

    This method attempts to automatically detect the encoding of a file based on the presence of byte order marks. If an encoding cannot be detected, the encoding specified by the caller in the method overload is used.

    Examples

    This example shows you how to use WriteTextAsync(String, String) to write some arbitrary text to a file at the path in absoluteFilePath, like this:

    Windows.Storage.FileIO.writeTextAsync(absoluteFilePath, "Swift as a shadow").then(function () {
        // Add code to do something after the text is written to the file
    });
    

    Although the WriteTextAsync(String, String) methods don't have a return value, you can still use then or done to declare a function and perform additional tasks after the text is written to the file, as the sample shows.

  • WriteTextAsync(String, String, UnicodeEncoding)
    WriteTextAsync(String, String, UnicodeEncoding)
    WriteTextAsync(String, String, UnicodeEncoding)
    WriteTextAsync(String, String, UnicodeEncoding)

    Writes text to the file at the specified path or Uniform Resource Identifier (URI) using the specified character encoding.

    public static IAsyncAction WriteTextAsync(String absolutePath, String contents, UnicodeEncoding encoding)public static IAsyncAction WriteTextAsync(String absolutePath, String contents, UnicodeEncoding encoding)Public Static Function WriteTextAsync(absolutePath As String, contents As String, encoding As UnicodeEncoding) As IAsyncActionpublic static IAsyncAction WriteTextAsync(String absolutePath, String contents, UnicodeEncoding encoding)

    Parameters

    • absolutePath
      System.String
      System.String
      System.String
      System.String

      The path of the file that the text is written to.

    • contents
      System.String
      System.String
      System.String
      System.String

      The text to write.

    • encoding

      The character encoding of the file.

    Returns

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

Windows.Foundation.Metadata.ContractVersionAttribute
Windows.Foundation.Metadata.MarshalingBehaviorAttribute
Windows.Foundation.Metadata.StaticAttribute
Windows.Foundation.Metadata.ThreadingAttribute

Details

Assembly

Windows.Storage.dll