Delen via


FileIO.ReadBufferAsync(IStorageFile) Method

Definition

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

public:
 static IAsyncOperation<IBuffer ^> ^ ReadBufferAsync(IStorageFile ^ file);
/// [Windows.Foundation.Metadata.RemoteAsync]
 static IAsyncOperation<IBuffer> ReadBufferAsync(IStorageFile const& file);
[Windows.Foundation.Metadata.RemoteAsync]
public static IAsyncOperation<IBuffer> ReadBufferAsync(IStorageFile file);
function readBufferAsync(file)
Public Shared Function ReadBufferAsync (file As IStorageFile) As IAsyncOperation(Of IBuffer)

Parameters

file
IStorageFile

The file to read.

Returns

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

Attributes

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
}

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

Applies to