FileDialogFileInfo.OpenRead Method

Creates a read-only Stream.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

public Stream OpenRead()

Return Value

Type: System.IO.Stream

A new read-only Stream object.

The following example shows how to open a file, read the first line, and write the line to a TextBox. For a full code listing, see How to: Use the Open File Dialog Box.

if (openFileDialog1.ShowDialog() == true)
{
    // Open the selected file to read.
    System.IO.Stream fileStream = openFileDialog1.SelectedFile.OpenRead();

    using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
    {
        // Read the first line from the file and write it the textbox.
        tbResults.Text = reader.ReadLine();
    }
    fileStream.Close();
}


Show: