PathIO PathIO PathIO PathIO Class

Definition

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

public : static class PathIO
public static class PathIO
Public Static Class PathIO
// This class does not provide a public constructor.
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Examples

You can use writeTextAsync(filePathOrURI, contents) 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 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(filePathOrURI) to read text from a file. After readTextAsync 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.

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.

Methods

AppendLinesAsync(String, IIterable) AppendLinesAsync(String, IIterable) AppendLinesAsync(String, IIterable) AppendLinesAsync(String, IIterable)

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

public : static IAsyncAction AppendLinesAsync(Platform::String absolutePath, IIterable<Platform::String> lines)
public static IAsyncAction AppendLinesAsync(String absolutePath, IEnumerable<String> lines)
Public Static Function AppendLinesAsync(absolutePath As String, lines As IEnumerable<String>) As IAsyncAction
var iAsyncAction = Windows.Storage.PathIO.appendLinesAsync(absolutePath, lines);
Parameters
absolutePath
Platform::String String String String

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

lines
IIterable<Platform::String> IEnumerable<String> IEnumerable<String> IEnumerable<String>

The list of text strings to append as lines.

Returns

No object or value is returned when this method completes.

See Also

AppendLinesAsync(String, IIterable, UnicodeEncoding) AppendLinesAsync(String, IIterable, UnicodeEncoding) AppendLinesAsync(String, IIterable, UnicodeEncoding) AppendLinesAsync(String, IIterable, 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(Platform::String absolutePath, IIterable<Platform::String> lines, UnicodeEncoding encoding)
public static IAsyncAction AppendLinesAsync(String absolutePath, IEnumerable<String> lines, UnicodeEncoding encoding)
Public Static Function AppendLinesAsync(absolutePath As String, lines As IEnumerable<String>, encoding As UnicodeEncoding) As IAsyncAction
var iAsyncAction = Windows.Storage.PathIO.appendLinesAsync(absolutePath, lines, encoding);
Parameters
absolutePath
Platform::String String String String

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

lines
IIterable<Platform::String> IEnumerable<String> IEnumerable<String> IEnumerable<String>

The list of text strings to append as lines.

encoding
UnicodeEncoding UnicodeEncoding UnicodeEncoding UnicodeEncoding

The character encoding of the file.

Returns

No object or value is returned when this method completes.

See Also

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(Platform::String absolutePath, Platform::String contents)
public static IAsyncAction AppendTextAsync(String absolutePath, String contents)
Public Static Function AppendTextAsync(absolutePath As String, contents As String) As IAsyncAction
var iAsyncAction = Windows.Storage.PathIO.appendTextAsync(absolutePath, contents);
Parameters
absolutePath
Platform::String String String String

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

contents
Platform::String String String String

The text to append.

Returns

No object or value is returned when this method completes.

See Also

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(Platform::String absolutePath, Platform::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 IAsyncAction
var iAsyncAction = Windows.Storage.PathIO.appendTextAsync(absolutePath, contents, encoding);
Parameters
absolutePath
Platform::String String String String

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

contents
Platform::String String String String

The text to append.

encoding
UnicodeEncoding UnicodeEncoding UnicodeEncoding UnicodeEncoding

The character encoding of the file.

Returns

No object or value is returned when this method completes.

See Also

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(Platform::String absolutePath)
public static IAsyncOperation<IBuffer> ReadBufferAsync(String absolutePath)
Public Static Function ReadBufferAsync(absolutePath As String) As IAsyncOperation( Of IBuffer )
var iAsyncOperation = Windows.Storage.PathIO.readBufferAsync(absolutePath);
Parameters
absolutePath
Platform::String String String 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<Platform::String>> ReadLinesAsync(Platform::String absolutePath)
public static IAsyncOperation<IList<string>> ReadLinesAsync(String absolutePath)
Public Static Function ReadLinesAsync(absolutePath As String) As IAsyncOperation<IList<string>>( Of IVector )
var iAsyncOperation = Windows.Storage.PathIO.readLinesAsync(absolutePath);
Parameters
absolutePath
Platform::String String String String

The path of the file to read.

Returns
IAsyncOperation<IVector<Platform::String>> IAsyncOperation<IList<string>> IAsyncOperation<IList<string>> IAsyncOperation<IList<string>>

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

See Also

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<Platform::String>> ReadLinesAsync(Platform::String absolutePath, UnicodeEncoding encoding)
public static IAsyncOperation<IList<string>> ReadLinesAsync(String absolutePath, UnicodeEncoding encoding)
Public Static Function ReadLinesAsync(absolutePath As String, encoding As UnicodeEncoding) As IAsyncOperation<IList<string>>( Of IVector )
var iAsyncOperation = Windows.Storage.PathIO.readLinesAsync(absolutePath, encoding);
Parameters
absolutePath
Platform::String String String String

The path of the file to read.

encoding
UnicodeEncoding UnicodeEncoding UnicodeEncoding UnicodeEncoding

The character encoding of the file.

Returns
IAsyncOperation<IVector<Platform::String>> IAsyncOperation<IList<string>> IAsyncOperation<IList<string>> IAsyncOperation<IList<string>>

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

See Also

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<Platform::String> ReadTextAsync(Platform::String absolutePath)
public static IAsyncOperation<string> ReadTextAsync(String absolutePath)
Public Static Function ReadTextAsync(absolutePath As String) As IAsyncOperation( Of string )
var iAsyncOperation = Windows.Storage.PathIO.readTextAsync(absolutePath);
Parameters
absolutePath
Platform::String String String 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.

Examples

This example shows you how to use readTextAsync(absolutePath) 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 method completes, the text will be passed to this function as a string object (contents in the example).

See Also

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<Platform::String> ReadTextAsync(Platform::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 )
var iAsyncOperation = Windows.Storage.PathIO.readTextAsync(absolutePath, encoding);
Parameters
absolutePath
Platform::String String String String

The path of the file to read.

encoding
UnicodeEncoding UnicodeEncoding UnicodeEncoding UnicodeEncoding

The character encoding of the file.

Returns

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

See Also

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(Platform::String absolutePath, IBuffer buffer)
public static IAsyncAction WriteBufferAsync(String absolutePath, IBuffer buffer)
Public Static Function WriteBufferAsync(absolutePath As String, buffer As IBuffer) As IAsyncAction
var iAsyncAction = Windows.Storage.PathIO.writeBufferAsync(absolutePath, buffer);
Parameters
absolutePath
Platform::String String String String

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

buffer
IBuffer IBuffer IBuffer IBuffer

The buffer that contains the data to write.

Returns

No object or value is returned when this method completes.

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(Platform::String absolutePath, Byte[] buffer)
public static IAsyncAction WriteBytesAsync(String absolutePath, Byte[] buffer)
Public Static Function WriteBytesAsync(absolutePath As String, buffer As Byte[]) As IAsyncAction
var iAsyncAction = Windows.Storage.PathIO.writeBytesAsync(absolutePath, buffer);
Parameters
absolutePath
Platform::String String String String

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

buffer
Byte[] Byte[] Byte[] Byte[]

An array of bytes to write.

Returns

No object or value is returned when this method completes.

WriteLinesAsync(String, IIterable) WriteLinesAsync(String, IIterable) WriteLinesAsync(String, IIterable) WriteLinesAsync(String, IIterable)

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

public : static IAsyncAction WriteLinesAsync(Platform::String absolutePath, IIterable<Platform::String> lines)
public static IAsyncAction WriteLinesAsync(String absolutePath, IEnumerable<String> lines)
Public Static Function WriteLinesAsync(absolutePath As String, lines As IEnumerable<String>) As IAsyncAction
var iAsyncAction = Windows.Storage.PathIO.writeLinesAsync(absolutePath, lines);
Parameters
absolutePath
Platform::String String String String

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

lines
IIterable<Platform::String> IEnumerable<String> IEnumerable<String> IEnumerable<String>

The list of text strings to append as lines.

Returns

No object or value is returned when this method completes.

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.

See Also

WriteLinesAsync(String, IIterable, UnicodeEncoding) WriteLinesAsync(String, IIterable, UnicodeEncoding) WriteLinesAsync(String, IIterable, UnicodeEncoding) WriteLinesAsync(String, IIterable, 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(Platform::String absolutePath, IIterable<Platform::String> lines, UnicodeEncoding encoding)
public static IAsyncAction WriteLinesAsync(String absolutePath, IEnumerable<String> lines, UnicodeEncoding encoding)
Public Static Function WriteLinesAsync(absolutePath As String, lines As IEnumerable<String>, encoding As UnicodeEncoding) As IAsyncAction
var iAsyncAction = Windows.Storage.PathIO.writeLinesAsync(absolutePath, lines, encoding);
Parameters
absolutePath
Platform::String String String String

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

lines
IIterable<Platform::String> IEnumerable<String> IEnumerable<String> IEnumerable<String>

The list of text strings to append as lines.

encoding
UnicodeEncoding UnicodeEncoding UnicodeEncoding UnicodeEncoding

The character encoding of the file.

Returns

No object or value is returned when this method completes.

See Also

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(Platform::String absolutePath, Platform::String contents)
public static IAsyncAction WriteTextAsync(String absolutePath, String contents)
Public Static Function WriteTextAsync(absolutePath As String, contents As String) As IAsyncAction
var iAsyncAction = Windows.Storage.PathIO.writeTextAsync(absolutePath, contents);
Parameters
absolutePath
Platform::String String String String

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

contents
Platform::String String String String

The text to write.

Returns

No object or value is returned when this method completes.

Examples

This example shows you how to use writeTextAsync(absolutePath, contents) 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 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.

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.

See Also

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(Platform::String absolutePath, Platform::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 IAsyncAction
var iAsyncAction = Windows.Storage.PathIO.writeTextAsync(absolutePath, contents, encoding);
Parameters
absolutePath
Platform::String String String String

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

contents
Platform::String String String String

The text to write.

encoding
UnicodeEncoding UnicodeEncoding UnicodeEncoding UnicodeEncoding

The character encoding of the file.

Returns

No object or value is returned when this method completes.

See Also

See Also