FileIO.ReadBufferAsync(IStorageFile) FileIO.ReadBufferAsync(IStorageFile) FileIO.ReadBufferAsync(IStorageFile) FileIO.ReadBufferAsync(IStorageFile) FileIO.ReadBufferAsync(IStorageFile) Method

Definition

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

public : static IAsyncOperation<IBuffer> ReadBufferAsync(IStorageFile file)
static IAsyncOperation<IBuffer> ReadBufferAsync(IStorageFile file) const;
public static IAsyncOperation<IBuffer> ReadBufferAsync(IStorageFile file)
Public Shared Function ReadBufferAsync(file As IStorageFile) As IAsyncOperation(Of IBuffer)
Windows.Storage.FileIO.readBufferAsync(file).done( /* Your success and error handlers */ );
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.

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.)