Export (0) Print
Expand All
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

DataReader class

Applies to Windows and Windows Phone

Reads data from an input stream.


public ref class DataReader sealed : IDataReader,




The DataReader class has these types of members:


The DataReader class has these constructors.

DataReader Creates and initializes a new instance of the data reader.



The DataReader class has these methods. With C#, Visual Basic, and C++, it also inherits methods from the Object class.

Close [C++, JavaScript]Closes the current stream and releases system resources.
DetachBuffer Detaches the buffer that is associated with the data reader.
DetachStream Detaches the stream that is associated with the data reader.
Dispose [C#, VB]Performs tasks associated with freeing, releasing, or resetting unmanaged resources.
FromBuffer Creates a new instance of the data reader with data from the specified buffer.
LoadAsync Loads data from the input stream.
ReadBoolean Reads a Boolean value from the input stream.
ReadBuffer Reads a buffer from the input stream.
ReadByte Reads a byte value from the input stream.
ReadBytes Reads an array of byte values from the input stream.
ReadDateTime Reads a date and time value from the input stream.
ReadDouble Reads a floating-point value from the input stream.
ReadGuid Reads a GUID value from the input stream.
ReadInt16 Reads a 16-bit integer value from the input stream.
ReadInt32 Reads a 32-bit integer value from the input stream.
ReadInt64 Reads a 64-bit integer value from the input stream.
ReadSingle Reads a floating-point value from the input stream.
ReadString Reads a string value from the input stream.
ReadTimeSpan Reads a time-interval value from the input stream.
ReadUInt16 Reads a 16-bit unsigned integer from the input stream.
ReadUInt32 Reads a 32-bit unsigned integer from the input stream.
ReadUInt64 Reads a 64-bit unsigned integer from the input stream.



The DataReader class has these properties.

PropertyAccess typeDescription


Read/writeGets or sets the byte order of the data in the input stream.


Read/writeGets or sets the read options for the input stream.


Read-onlyGets the size of the buffer that has not been read.


Read/writeGets or sets the Unicode character encoding for the input stream.



The following example shows how to write and read strings to an in-memory stream. For the full code sample, see Reading and writing data sample.

#include "pch.h"
#include "WriteReadStream.xaml.h"

using namespace Concurrency;
using namespace DataReaderWriter;
using namespace Platform;
using namespace Windows::Storage::Streams;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Navigation;

Array<String^>^ _inputElements = ref new Array<String^>
    "Hello", "World", "1 2 3 4 5", "Très bien!", "Goodbye"


    // Populate the text block with the input elements.
    ElementsToWrite->Text = "";
    for (unsigned int i = 0; i < _inputElements->Length; i++)
        ElementsToWrite->Text += _inputElements[i] + ";";

// Invoked when this page is about to be displayed in a Frame.
void WriteReadStream::OnNavigatedTo(NavigationEventArgs^ e)
    // Get a pointer to our main page.
    rootPage = MainPage::Current;

// This is the click handler for the 'Copy Strings' button.  Here we will parse the
// strings contained in the ElementsToWrite text block, write them to a stream using
// DataWriter, retrieve them using DataReader, and output the results in the
// ElementsRead text block.
void DataReaderWriter::WriteReadStream::TransferData(
Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    // Initialize the in-memory stream where data will be stored.
    InMemoryRandomAccessStream^ stream = ref new InMemoryRandomAccessStream();

    // Create the DataWriter object backed by the in-memory stream.  When
    // dataWriter is deleted, it will also close the underlying stream.
    DataWriter^ dataWriter = ref new DataWriter(stream);
    dataWriter->UnicodeEncoding = UnicodeEncoding::Utf8;
    dataWriter->ByteOrder = ByteOrder::LittleEndian;

    // Create the data reader by using the input stream set at position 0 so that 
    // the stream will be read from the beginning regardless of where the position
    // the original stream ends up in after the store.
    IInputStream^ inputStream = stream->GetInputStreamAt(0);
    DataReader^ dataReader = ref new DataReader(inputStream);
    // The encoding and byte order need to match the settings of the writer 
    / we previously used.
    dataReader->UnicodeEncoding = UnicodeEncoding::Utf8;
    dataReader->ByteOrder = ByteOrder::LittleEndian;

    // Write the input data to the output stream.  Serialize the elements by writing
    // each string separately, preceded by its length.
    for (unsigned int i = 0; i < _inputElements->Length; i++) 
        unsigned int inputElementSize = dataWriter->MeasureString(_inputElements[i]);

    // Send the contents of the writer to the backing stream.
    create_task(dataWriter->StoreAsync()).then([this, dataWriter] (unsigned int bytesStored)
        // For the in-memory stream implementation we are using, the FlushAsync() call 
        // is superfluous, but other types of streams may require it.
        return dataWriter->FlushAsync();
    }).then([this, dataReader, stream] (bool flushOp)
        // Once we have written the contents successfully we load the stream.
        return dataReader->LoadAsync((unsigned int) stream->Size);
    }).then([this, dataReader] (task<unsigned int> bytesLoaded)
            // Check for possible exceptions that could have been thrown 
            // in the async call chain.

            String^ readFromStream = "";

            // Keep reading until we consume the complete stream.
            while (dataReader->UnconsumedBufferLength > 0)
                // Note that the call to ReadString requires a length of 
                // "code units" to read. This is the reason each string is 
                // preceded by its length when "on the wire".
                unsigned int bytesToRead = dataReader->ReadUInt32();
                readFromStream += dataReader->ReadString(bytesToRead) + "\n";

            // Populate the ElementsRead text block with the items we read from the stream
            ElementsRead->Text = readFromStream;
        catch (Exception^ e)
            ElementsRead->Text = "Error: " + e->Message;


Minimum supported client

Windows 8 [Windows Store apps only]

Minimum supported server

Windows Server 2012 [Windows Store apps only]

Minimum supported phone

Windows Phone 8


Windows::Storage::Streams [C++]



See also

Reading and writing data sample
StreamSocket sample



© 2015 Microsoft