Skip to content

Latest commit

 

History

History
54 lines (41 loc) · 1.78 KB

fileio_readbufferasync_511423996.md

File metadata and controls

54 lines (41 loc) · 1.78 KB
-api-id -api-type
M:Windows.Storage.FileIO.ReadBufferAsync(Windows.Storage.IStorageFile)
winrt method

Windows.Storage.FileIO.ReadBufferAsync

-description

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

-parameters

-param file

The file to read.

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

-see-also