Export (0) Print
Expand All

BinaryReader.ReadString Method

Reads a string from the current stream. The string is prefixed with the length, encoded as an integer seven bits at a time.

[Visual Basic]
Public Overridable Function ReadString() As String
[C#]
public virtual string ReadString();
[C++]
public: virtual String* ReadString();
[JScript]
public function ReadString() : String;

Return Value

The string being read.

Exceptions

Exception Type Condition
EndOfStreamException The end of the stream is reached.
ObjectDisposedException The stream is closed.
IOException An I/O error occurs.

Remarks

The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File

File.AppendText

FileInfo.AppendText

Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File

Example

[Visual Basic, C#, C++] This code example is part of a larger example provided for the BinaryReader class.

[Visual Basic] 
Dim binReader As New BinaryReader( _
    File.Open(fileName, FileMode.Open))
Try

    ' If the file is not empty, 
    ' read the application settings.
    If binReader.PeekChar() <> -1 Then
        aspRatio   = binReader.ReadSingle()
        lkupDir     = binReader.ReadString()
        saveTime  = binReader.ReadInt32()
        statusBar = binReader.ReadBoolean()
        Return
    End If

' If the end of the stream is reached before reading
' the four data values, ignore the error and use the
' default settings for the remaining values.
Catch ex As EndOfStreamException
    Console.WriteLine("{0} caught and ignored. " & _ 
        "Using default values.", ex.GetType().Name)
Finally
    binReader.Close()
End Try

[C#] 
BinaryReader binReader = 
    new BinaryReader(File.Open(fileName, FileMode.Open));
try
{
    // If the file is not empty, 
    // read the application settings.
    if(binReader.PeekChar() != -1)
    {
        aspectRatio   = binReader.ReadSingle();
        lookupDir     = binReader.ReadString();
        autoSaveTime  = binReader.ReadInt32();
        showStatusBar = binReader.ReadBoolean();
    }
}

// If the end of the stream is reached before reading
// the four data values, ignore the error and use the
// default settings for the remaining values.
catch(EndOfStreamException e)
{
    Console.WriteLine("{0} caught and ignored. " + 
        "Using default values.", e.GetType().Name);
}
finally
{
    binReader.Close();
}

[C++] 
BinaryReader* binReader = 
    new BinaryReader(File::Open(fileName, FileMode::Open));
try
{
    // If the file is not empty, 
    // read the application settings.
    if(binReader->PeekChar() != -1)
    {
        aspectRatio   = binReader->ReadSingle();
        lookupDir     = binReader->ReadString();
        autoSaveTime  = binReader->ReadInt32();
        showStatusBar = binReader->ReadBoolean();
        return;
    }
}

// If the end of the stream is reached before reading
// the four data values, ignore the error and use the
// default settings for the remaining values.
catch(EndOfStreamException* e)
{
    Console::WriteLine(S"{0} caught and ignored. "
        S"Using default values.", e->GetType()->Name);
}
__finally
{
    binReader->Close();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

BinaryReader Class | BinaryReader Members | System.IO Namespace | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2014 Microsoft