-api-id | -api-type |
---|---|
M:Windows.Storage.FileIO.ReadBufferAsync(Windows.Storage.IStorageFile) |
winrt method |
Reads the contents of the specified file and returns a buffer.
The file to read.
When this method completes, it returns an object (type IBuffer) that represents the contents of the file.
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.)