Share via


StorageStreamTransaction Class

Definition

Represents a write transaction for a random-access stream.

public ref class StorageStreamTransaction sealed : IClosable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
class StorageStreamTransaction final : IClosable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
public sealed class StorageStreamTransaction : System.IDisposable
Public NotInheritable Class StorageStreamTransaction
Implements IDisposable
Inheritance
Object Platform::Object IInspectable StorageStreamTransaction
Attributes
Implements

Windows requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

The File Access sample shows you how to use a StorageStreamTransaction object to perform a transacted write operation on a file.

try
{
    if (file != null)
    {
        using (StorageStreamTransaction transaction = await file.OpenTransactedWriteAsync())
        {
            using (DataWriter dataWriter = new DataWriter(transaction.Stream))
            {
                dataWriter.WriteString("Swift as a shadow");
                transaction.Stream.Size = await dataWriter.StoreAsync(); // reset stream size to override the file
                await transaction.CommitAsync();
            }
        }
    }
}
// Use catch blocks to handle errors
catch (FileNotFoundException)
{
    // For example, handle a file not found error
}

In the example, file is a local variable that contains a StorageFile that represents the file to write.

Properties

Stream

Gets the random-access stream used in the transaction.

Methods

Close()

Releases system resources that are exposed by the stream, indicating that the data request is complete.

CommitAsync()

Save the stream to the underlying file.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

Applies to

See also