Sign in to follow this  

VB.NET casting to System.IO.StreamReader

This topic is 4378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an open file Dialog box and I am trying to get the stream from that dialog box, but I need to cast from a Stream to a StreamReader, but when I do I am getting this error: "error BC30311: Value of type 'System.IO.Stream' cannot be converted to 'System.IO.StreamReader'" here's my code snippet:
            Dim FileDiag As New OpenFileDialog
            FileDiag.InitialDirectory = "C:\"
            FileDiag.Filter = "Text files (*.txt)|*.txt"
            FileDiag.FilterIndex = 1
            FileDiag.RestoreDirectory = True

            If FileDiag.ShowDialog() = DialogResult.OK Then
                'The error is on the line below
                m_InputStream = CType(FileDiag.OpenFile(), System.IO.StreamReader)
                If Me.m_InputStream Is Nothing Then
                    Throw New Exception("Error opening the file")
                End If

                Me.m_InputFilename.Text = FileDiag.FileName

            End If

what am I doing wrong? ~guyaton

Share this post


Link to post
Share on other sites
OpenFile returns a Stream, pass this to the StreamReader constructor to read from it, something like this:

m_InputStream = new System.IO.StreamReader(FileDiag.OpenFile())

Share this post


Link to post
Share on other sites

This topic is 4378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this