FileIO.WriteBufferAsync(IStorageFile, IBuffer) Method

Definition

Writes data from a buffer to the specified file.

public:
 static IAsyncAction ^ WriteBufferAsync(IStorageFile ^ file, IBuffer ^ buffer);
 static IAsyncAction WriteBufferAsync(IStorageFile const & file, IBuffer const & buffer);
[Windows.Foundation.Metadata.RemoteAsync]
public static IAsyncAction WriteBufferAsync(IStorageFile file, IBuffer buffer);
function writeBufferAsync(file, buffer)
Public Shared Function WriteBufferAsync (file As IStorageFile, buffer As IBuffer) As IAsyncAction

Parameters

file
IStorageFile

The file that the buffer of data is written to.

buffer
IBuffer

The buffer that contains the data to write.

Returns

IAsyncAction

No object or value is returned when this method completes.

Attributes

Examples

The File Access sample shows you how to use WriteBufferAsync to write data from a buffer to a file.


try
{
    if (file != null)
    {
        IBuffer buffer = CryptographicBuffer.GenerateRandom(10);
        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 buffer = Windows.Security.Cryptography.CryptographicBuffer.generateRandom(10);

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

Applies to