StreamedFileDataRequestedHandler Delegate

Definition

Represents the method that will stream data into a StorageFile when that file is first accessed.

C#
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.Guid(4277577764, 12257, 19719, 163, 91, 183, 124, 80, 181, 244, 204)]
public delegate void StreamedFileDataRequestedHandler(StreamedFileDataRequest stream);

Parameters

stream
StreamedFileDataRequest

The request for the streamed data in the StorageFile that was created by the CreateStreamedFileAsync or ReplaceWithStreamedFileAsync method.

Attributes

Windows requirements

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

Examples

The following simple example demonstrates how to create an instance of the StreamedFileDataRequestedHandler delegate. The example also shows how this instance produces the data stream for the StorageFile that was returned by the CreateStreamedFileAsync when the file is first accessed.

This simple example is intended only to demonstrate the programming pattern for using the StreamedFileDataRequestedHandler delegate. For a small file with data that's easy to produce, the streaming pattern is not useful.

C#
using Windows.Storage;
using Windows.Storage.Streams;

private async void CreateStreamedFile()
{
    // Create a streamed file.
    StorageFile file =
    await StorageFile.CreateStreamedFileAsync("file.txt",
        StreamedFileWriter, null);

    // Prepare to copy the file.
    StorageFolder localFolder = ApplicationData.Current.LocalFolder;
    string newName = "copied_file.txt";

    // Copy the streamed file. At this point,
    // the data is streamed into the source file.
    await file.CopyAsync(localFolder, newName,
        NameCollisionOption.ReplaceExisting);
}

private async void StreamedFileWriter(StreamedFileDataRequest request)
{
    try
    {
        using (var stream = request.AsStreamForWrite())
        using (var streamWriter = new StreamWriter(stream))
        {
            for (int l = 0; l < 50; l++)
            {
                await streamWriter.WriteLineAsync("Data.");
            }
        }
        request.Dispose();
    }
    catch (Exception ex)
    {
        request.FailAndClose(StreamedFileFailureMode.Incomplete);
    }
}

Remarks

The instance of the delegate that you create will produce the data stream for the StorageFile that was returned by the CreateStreamedFileAsync or ReplaceWithStreamedFileAsync method.

When you use the CreateStreamedFileAsync or ReplaceWithStreamedFileAsync method together with an instance of StreamedFileDataRequestedHandler, you can provide the data for a file on demand instead of writing the contents of a file when you create it. That is, you can defer expensive operations to produce the data for the file until the file is first accessed.

Applies to

Product Versions
WinRT Build 10240, Build 10586, Build 14383, Build 15063, Build 16299, Build 17134, Build 17763, Build 18362, Build 19041, Build 20348, Build 22000, Build 22621, Build 26100