File​IO File​IO File​IO Class

Definition

Provides helper methods for reading and writing files that are represented by objects of type IStorageFile.

public static class FileIOpublic static class FileIOPublic Static Class FileIO
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Remarks

This class is static and cannot be instantiated. Call the methods directly instead.

To learn more about what locations your app can access, see File access permissions.

To learn how to read and write to files, see Quickstart: Reading and writing a file.

Examples

The File Access sample shows you how to use writeTextAsync(file, contents) to write text to a file.


if (file !== null) {
    Windows.Storage.FileIO.writeTextAsync(file, "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
    });
}

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

In the example, file is a local variable that contains a storageFile that represents the file to write.

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.The File Access sample also shows you how to use readTextAsync(file) to read text from a file.

if (file !== null) {
    Windows.Storage.FileIO.readTextAsync(file).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 FileIO.ReadTextAsync(file);
    }
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
    // For example, handle file not found
}

In the example, file is a local variable that contains a storageFile that represents the file to read.

After readTextAsync completes, the fileContent variable gets the contents of the file as a text string. You can then process the contents as appropriate.

Methods

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

Appends lines of text to the specified file.

public static IAsyncAction AppendLinesAsync(IStorageFile file, IIterable<String> lines)public static IAsyncAction AppendLinesAsync(IStorageFile file, IIterable<String> lines)Public Static Function AppendLinesAsync(file As IStorageFile, lines As IIterable<String>) As IAsyncAction
Parameters
file
IStorageFile IStorageFile IStorageFile

The file that the lines are appended to.

lines

The list of text strings to append as lines.

Returns

No object or value is returned when this method completes.

Attributes

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

Appends lines of text to the specified file using the specified character encoding.

public static IAsyncAction AppendLinesAsync(IStorageFile file, IIterable<String> lines, UnicodeEncoding encoding)public static IAsyncAction AppendLinesAsync(IStorageFile file, IIterable<String> lines, UnicodeEncoding encoding)Public Static Function AppendLinesAsync(file As IStorageFile, lines As IIterable<String>, encoding As UnicodeEncoding) As IAsyncAction
Parameters
file
IStorageFile IStorageFile IStorageFile

The file that the lines are appended to.

lines

The list of text strings to append as lines.

encoding
UnicodeEncoding UnicodeEncoding UnicodeEncoding

The character encoding of the file.

Returns

No object or value is returned when this method completes.

Attributes

AppendTextAsync(IStorageFile, String) AppendTextAsync(IStorageFile, String) AppendTextAsync(IStorageFile, String)

Appends text to the specified file.

public static IAsyncAction AppendTextAsync(IStorageFile file, String contents)public static IAsyncAction AppendTextAsync(IStorageFile file, String contents)Public Static Function AppendTextAsync(file As IStorageFile, contents As String) As IAsyncAction
Parameters
file
IStorageFile IStorageFile IStorageFile

The file that the text is appended to.

contents
System.String System.String System.String

The text to append.

Returns

No object or value is returned when this method completes.

Attributes

AppendTextAsync(IStorageFile, String, UnicodeEncoding) AppendTextAsync(IStorageFile, String, UnicodeEncoding) AppendTextAsync(IStorageFile, String, UnicodeEncoding)

Appends text to the specified file using the specified character encoding.

public static IAsyncAction AppendTextAsync(IStorageFile file, String contents, UnicodeEncoding encoding)public static IAsyncAction AppendTextAsync(IStorageFile file, String contents, UnicodeEncoding encoding)Public Static Function AppendTextAsync(file As IStorageFile, contents As String, encoding As UnicodeEncoding) As IAsyncAction
Parameters
file
IStorageFile IStorageFile IStorageFile

The file that the text is appended to.

contents
System.String System.String System.String

The text to append.

encoding
UnicodeEncoding UnicodeEncoding UnicodeEncoding

The character encoding of the file.

Returns

No object or value is returned when this method completes.

Attributes

ReadBufferAsync(IStorageFile) ReadBufferAsync(IStorageFile) ReadBufferAsync(IStorageFile)

Reads the contents of the specified file and returns a buffer.

public static IAsyncOperation<IBuffer> ReadBufferAsync(IStorageFile file)public static IAsyncOperation<IBuffer> ReadBufferAsync(IStorageFile file)Public Static Function ReadBufferAsync(file As IStorageFile) As IAsyncOperation( Of IBuffer )
Parameters
file
IStorageFile IStorageFile IStorageFile

The file to read.

Returns

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

Attributes

Remarks

Examples

The File Access sample shows you how to use ReadBufferAsync to read the contents of a file and return a buffer, like this:


try
{
    if (file != null)
    {
        IBuffer buffer = await FileIO.ReadBufferAsync(file);

        // Use a dataReader object to read from the buffer
        using (DataReader dataReader = DataReader.FromBuffer(buffer))
        {
            string fileContent = dataReader.ReadString(buffer.Length);
            // Perform additional tasks
        }
    }
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
    // For example, handle file not found
}

if (file !== null) {
    Windows.Storage.FileIO.readBufferAsync(sampleFile).then(function (buffer) {
        // Perform additional tasks after file is read

        // Use a dataReader object to read from the buffer
        var dataReader = Windows.Storage.Streams.DataReader.fromBuffer(buffer);
        var fileContent = dataReader.readString(buffer.length);
        dataReader.close();
    },
    // Handle errors with an error function
    function (error) {
        // Handle errors encountered during write
    });
}

In the example, file is a local variable that contains a storageFile that represents the file to read.

After readTextAsync completes, the buffer variable gets the contents of the file as an IBuffer object. You can then read from the buffer using a dataReader object and process the file contents as appropriate (as shown in the example.)

ReadLinesAsync(IStorageFile) ReadLinesAsync(IStorageFile) ReadLinesAsync(IStorageFile)

Reads the contents of the specified file and returns lines of text.

public static IAsyncOperation<IVector<string>> ReadLinesAsync(IStorageFile file)public static IAsyncOperation<IVector<string>> ReadLinesAsync(IStorageFile file)Public Static Function ReadLinesAsync(file As IStorageFile) As IAsyncOperation( Of IVectorstring )
Parameters
file
IStorageFile IStorageFile IStorageFile

The file to read.

Returns

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.

Attributes

Remarks

This method uses the character encoding of the specified file. If you want to specify different encoding, call ReadLinesAsync(IStorageFile, UnicodeEncoding) instead.

ReadLinesAsync(IStorageFile, UnicodeEncoding) ReadLinesAsync(IStorageFile, UnicodeEncoding) ReadLinesAsync(IStorageFile, UnicodeEncoding)

Reads the contents of the specified file using the specified character encoding and returns lines of text.

public static IAsyncOperation<IVector<string>> ReadLinesAsync(IStorageFile file, UnicodeEncoding encoding)public static IAsyncOperation<IVector<string>> ReadLinesAsync(IStorageFile file, UnicodeEncoding encoding)Public Static Function ReadLinesAsync(file As IStorageFile, encoding As UnicodeEncoding) As IAsyncOperation( Of IVectorstring )
Parameters
file
IStorageFile IStorageFile IStorageFile

The file to read.

encoding
UnicodeEncoding UnicodeEncoding UnicodeEncoding

The character encoding to use.

Returns

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.

Attributes

ReadTextAsync(IStorageFile) ReadTextAsync(IStorageFile) ReadTextAsync(IStorageFile)

Reads the contents of the specified file and returns text.

public static IAsyncOperation<string> ReadTextAsync(IStorageFile file)public static IAsyncOperation<string> ReadTextAsync(IStorageFile file)Public Static Function ReadTextAsync(file As IStorageFile) As IAsyncOperation( Of string )
Parameters
file
IStorageFile IStorageFile IStorageFile

The file to read.

Returns

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

Attributes

Remarks

Any object that implements the IStorageFile interface may be passed to this method or its overload through the file parameter.

This method uses the character encoding of the specified file. If you want to specify different encoding, call ReadTextAsync(IStorageFile, UnicodeEncoding) instead.

ReadTextAsync(IStorageFile, UnicodeEncoding) ReadTextAsync(IStorageFile, UnicodeEncoding) ReadTextAsync(IStorageFile, UnicodeEncoding)

Reads the contents of the specified file using the specified character encoding and returns text.

public static IAsyncOperation<string> ReadTextAsync(IStorageFile file, UnicodeEncoding encoding)public static IAsyncOperation<string> ReadTextAsync(IStorageFile file, UnicodeEncoding encoding)Public Static Function ReadTextAsync(file As IStorageFile, encoding As UnicodeEncoding) As IAsyncOperation( Of string )
Parameters
file
IStorageFile IStorageFile IStorageFile

The file to read.

encoding
UnicodeEncoding UnicodeEncoding UnicodeEncoding

The character encoding to use.

Returns

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

Attributes

WriteBufferAsync(IStorageFile, IBuffer) WriteBufferAsync(IStorageFile, IBuffer) WriteBufferAsync(IStorageFile, IBuffer)

Writes data from a buffer to the specified file.

public static IAsyncAction WriteBufferAsync(IStorageFile file, IBuffer buffer)public static IAsyncAction WriteBufferAsync(IStorageFile file, IBuffer buffer)Public Static Function WriteBufferAsync(file As IStorageFile, buffer As IBuffer) As IAsyncAction
Parameters
file
IStorageFile IStorageFile IStorageFile

The file that the buffer of data is written to.

buffer
IBuffer IBuffer IBuffer

The buffer that contains the data to write.

Returns

No object or value is returned when this method completes.

Attributes

Remarks

Examples

The File Access sample shows you how to use WriteBufferAsync to write data from a buffer to a file.


try
{
    if (file != null)
    {
        IBuffer buffer = GetBufferFromString("Swift as a shadow");
        await FileIO.WriteBufferAsync(file, buffer);
        // Perform additional tasks after file is written
    }
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
    // For example, handle file not found
}

if (file !== null) {
    // Create buffer
    var memoryStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
    var dataWriter = new Windows.Storage.Streams.DataWriter(memoryStream);
    dataWriter.writeString("Swift as a shadow");
    var buffer = dataWriter.detachBuffer();
    dataWriter.close();

    // Write buffer to file
    Windows.Storage.FileIO.writeBufferAsync(file, buffer).done(function () {
        // Perform additional tasks after file is written
    },
    // Handle errors with an error function
    function (error) {
        // Handle errors encountered during write
    });
}

In the example, file is a local variable that contains a storageFile that represents the file to write.

Although the writeBufferAsync 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.

WriteBytesAsync(IStorageFile, Byte[]) WriteBytesAsync(IStorageFile, Byte[]) WriteBytesAsync(IStorageFile, Byte[])

Writes an array of bytes of data to the specified file.

public static IAsyncAction WriteBytesAsync(IStorageFile file, Byte[] buffer)public static IAsyncAction WriteBytesAsync(IStorageFile file, Byte[] buffer)Public Static Function WriteBytesAsync(file As IStorageFile, buffer As Byte[]) As IAsyncAction
Parameters
file
IStorageFile IStorageFile IStorageFile

The file that the byte is written to.

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

The array of bytes to write.

Returns

No object or value is returned when this method completes.

Attributes

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

Writes lines of text to the specified file.

public static IAsyncAction WriteLinesAsync(IStorageFile file, IIterable<String> lines)public static IAsyncAction WriteLinesAsync(IStorageFile file, IIterable<String> lines)Public Static Function WriteLinesAsync(file As IStorageFile, lines As IIterable<String>) As IAsyncAction
Parameters
file
IStorageFile IStorageFile IStorageFile

The file that the lines are written to.

lines

The list of text strings to write as lines.

Returns

No object or value is returned when this method completes.

Attributes

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 is used.

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

Writes lines of text to the specified file using the specified character encoding.

public static IAsyncAction WriteLinesAsync(IStorageFile file, IIterable<String> lines, UnicodeEncoding encoding)public static IAsyncAction WriteLinesAsync(IStorageFile file, IIterable<String> lines, UnicodeEncoding encoding)Public Static Function WriteLinesAsync(file As IStorageFile, lines As IIterable<String>, encoding As UnicodeEncoding) As IAsyncAction
Parameters
file
IStorageFile IStorageFile IStorageFile

The file that the lines are written to.

lines

The list of text strings to write as lines.

encoding
UnicodeEncoding UnicodeEncoding UnicodeEncoding

The character encoding of the file.

Returns

No object or value is returned when this method completes.

Attributes

WriteTextAsync(IStorageFile, String) WriteTextAsync(IStorageFile, String) WriteTextAsync(IStorageFile, String)

Writes text to the specified file.

public static IAsyncAction WriteTextAsync(IStorageFile file, String contents)public static IAsyncAction WriteTextAsync(IStorageFile file, String contents)Public Static Function WriteTextAsync(file As IStorageFile, contents As String) As IAsyncAction
Parameters
file
IStorageFile IStorageFile IStorageFile

The file that the text is written to.

contents
System.String System.String System.String

The text to write.

Returns

No object or value is returned when this method completes.

Attributes

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 is used.

WriteTextAsync(IStorageFile, String, UnicodeEncoding) WriteTextAsync(IStorageFile, String, UnicodeEncoding) WriteTextAsync(IStorageFile, String, UnicodeEncoding)

Writes text to the specified file using the specified character encoding.

public static IAsyncAction WriteTextAsync(IStorageFile file, String contents, UnicodeEncoding encoding)public static IAsyncAction WriteTextAsync(IStorageFile file, String contents, UnicodeEncoding encoding)Public Static Function WriteTextAsync(file As IStorageFile, contents As String, encoding As UnicodeEncoding) As IAsyncAction
Parameters
file
IStorageFile IStorageFile IStorageFile

The file that the text is written to.

contents
System.String System.String System.String

The text to write.

encoding
UnicodeEncoding UnicodeEncoding UnicodeEncoding

The character encoding of the file.

Returns

No object or value is returned when this method completes.

Attributes