FileIO FileIO FileIO FileIO Class

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

Syntax

Declaration

public static class FileIOpublic static class FileIOPublic Static Class FileIO

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(Windows.Storage.IStorageFile,System.String) 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(Windows.Storage.IStorageFile,System.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.The File Access sample also shows you how to use ReadTextAsync(Windows.Storage.IStorageFile) 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(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding) completes, the fileContent variable gets the contents of the file as a text string. You can then process the contents as appropriate.

Methods summary

Appends lines of text to the specified file.

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

Appends text to the specified file.

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

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

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

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

Reads the contents of the specified file and returns text.

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

Writes data from a buffer to the specified file.

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

Writes lines of text to the specified file.

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

Writes text to the specified file.

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

Methods

  • AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String})
    AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String})
    AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String})
    AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String})

    Appends lines of text to the specified file.

    public static IAsyncAction AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String})public static IAsyncAction AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String})Public Static Function AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String}) As IAsyncAction

    Parameters

    Returns

  • AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding)
    AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding)
    AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding)
    AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding)

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

    public static IAsyncAction AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding)public static IAsyncAction AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding)Public Static Function AppendLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding) As IAsyncAction

    Parameters

    Returns

  • AppendTextAsync(Windows.Storage.IStorageFile,System.String)
    AppendTextAsync(Windows.Storage.IStorageFile,System.String)
    AppendTextAsync(Windows.Storage.IStorageFile,System.String)
    AppendTextAsync(Windows.Storage.IStorageFile,System.String)

    Appends text to the specified file.

    public static IAsyncAction AppendTextAsync(Windows.Storage.IStorageFile,System.String)public static IAsyncAction AppendTextAsync(Windows.Storage.IStorageFile,System.String)Public Static Function AppendTextAsync(Windows.Storage.IStorageFile,System.String) As IAsyncAction

    Parameters

    Returns

  • AppendTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding)
    AppendTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding)
    AppendTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding)
    AppendTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding)

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

    public static IAsyncAction AppendTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding)public static IAsyncAction AppendTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding)Public Static Function AppendTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding) As IAsyncAction

    Parameters

    Returns

  • ReadBufferAsync(Windows.Storage.IStorageFile)
    ReadBufferAsync(Windows.Storage.IStorageFile)
    ReadBufferAsync(Windows.Storage.IStorageFile)
    ReadBufferAsync(Windows.Storage.IStorageFile)

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

    public static IAsyncOperation<IBuffer> ReadBufferAsync(Windows.Storage.IStorageFile)public static IAsyncOperation<IBuffer> ReadBufferAsync(Windows.Storage.IStorageFile)Public Static Function ReadBufferAsync(Windows.Storage.IStorageFile) As IAsyncOperation( Of IBuffer )

    Parameters

    Returns

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

    Remarks

    Examples

    The File Access sample shows you how to use ReadBufferAsync(Windows.Storage.IStorageFile) 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(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding) 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(Windows.Storage.IStorageFile)
    ReadLinesAsync(Windows.Storage.IStorageFile)
    ReadLinesAsync(Windows.Storage.IStorageFile)
    ReadLinesAsync(Windows.Storage.IStorageFile)

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

    public static IAsyncOperation<IVector<string>> ReadLinesAsync(Windows.Storage.IStorageFile)public static IAsyncOperation<IVector<string>> ReadLinesAsync(Windows.Storage.IStorageFile)Public Static Function ReadLinesAsync(Windows.Storage.IStorageFile) As IAsyncOperation( Of IVectorstring )

    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.

    Remarks

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

  • ReadLinesAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding)
    ReadLinesAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding)
    ReadLinesAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding)
    ReadLinesAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding)

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

    public static IAsyncOperation<IVector<string>> ReadLinesAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding)public static IAsyncOperation<IVector<string>> ReadLinesAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding)Public Static Function ReadLinesAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding) As IAsyncOperation( Of IVectorstring )

    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(Windows.Storage.IStorageFile)
    ReadTextAsync(Windows.Storage.IStorageFile)
    ReadTextAsync(Windows.Storage.IStorageFile)
    ReadTextAsync(Windows.Storage.IStorageFile)

    Reads the contents of the specified file and returns text.

    public static IAsyncOperation<string> ReadTextAsync(Windows.Storage.IStorageFile)public static IAsyncOperation<string> ReadTextAsync(Windows.Storage.IStorageFile)Public Static Function ReadTextAsync(Windows.Storage.IStorageFile) As IAsyncOperation( Of string )

    Parameters

    Returns

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

    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(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding) instead.

  • ReadTextAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding)
    ReadTextAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding)
    ReadTextAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding)
    ReadTextAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding)

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

    public static IAsyncOperation<string> ReadTextAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding)public static IAsyncOperation<string> ReadTextAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding)Public Static Function ReadTextAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.UnicodeEncoding) As IAsyncOperation( Of string )

    Parameters

    Returns

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

  • WriteBufferAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.IBuffer)
    WriteBufferAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.IBuffer)
    WriteBufferAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.IBuffer)
    WriteBufferAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.IBuffer)

    Writes data from a buffer to the specified file.

    public static IAsyncAction WriteBufferAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.IBuffer)public static IAsyncAction WriteBufferAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.IBuffer)Public Static Function WriteBufferAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.IBuffer) As IAsyncAction

    Parameters

    Returns

    Remarks

    Examples

    The File Access sample shows you how to use WriteBufferAsync(Windows.Storage.IStorageFile,Windows.Storage.Streams.IBuffer) 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(Windows.Storage.IStorageFile,Windows.Storage.Streams.IBuffer) 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(Windows.Storage.IStorageFile,System.Byte[])
    WriteBytesAsync(Windows.Storage.IStorageFile,System.Byte[])
    WriteBytesAsync(Windows.Storage.IStorageFile,System.Byte[])
    WriteBytesAsync(Windows.Storage.IStorageFile,System.Byte[])

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

    public static IAsyncAction WriteBytesAsync(Windows.Storage.IStorageFile,System.Byte[])public static IAsyncAction WriteBytesAsync(Windows.Storage.IStorageFile,System.Byte[])Public Static Function WriteBytesAsync(Windows.Storage.IStorageFile,System.Byte[]) As IAsyncAction

    Parameters

    Returns

  • WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String})
    WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String})
    WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String})
    WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String})

    Writes lines of text to the specified file.

    public static IAsyncAction WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String})public static IAsyncAction WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String})Public Static Function WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String}) As IAsyncAction

    Parameters

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

  • WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding)
    WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding)
    WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding)
    WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding)

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

    public static IAsyncAction WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding)public static IAsyncAction WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding)Public Static Function WriteLinesAsync(Windows.Storage.IStorageFile,Windows.Foundation.Collections.IIterable{System.String},Windows.Storage.Streams.UnicodeEncoding) As IAsyncAction

    Parameters

    Returns

  • WriteTextAsync(Windows.Storage.IStorageFile,System.String)
    WriteTextAsync(Windows.Storage.IStorageFile,System.String)
    WriteTextAsync(Windows.Storage.IStorageFile,System.String)
    WriteTextAsync(Windows.Storage.IStorageFile,System.String)

    Writes text to the specified file.

    public static IAsyncAction WriteTextAsync(Windows.Storage.IStorageFile,System.String)public static IAsyncAction WriteTextAsync(Windows.Storage.IStorageFile,System.String)Public Static Function WriteTextAsync(Windows.Storage.IStorageFile,System.String) As IAsyncAction

    Parameters

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

  • WriteTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding)
    WriteTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding)
    WriteTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding)
    WriteTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding)

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

    public static IAsyncAction WriteTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding)public static IAsyncAction WriteTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding)Public Static Function WriteTextAsync(Windows.Storage.IStorageFile,System.String,Windows.Storage.Streams.UnicodeEncoding) As IAsyncAction

    Parameters

    Returns

Device family

Windows 10 (introduced v10.0.10240.0)

API contract

Windows.Foundation.UniversalApiContract (introduced v1)

Attributes

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

Details

Assembly

Windows.Storage.dll