Results 1 to 8 of 8

Thread: Need help reading from a file

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2018
    Posts
    34

    Need help reading from a file

    I am trying to open a file and read the data contained in it, it is a txt file, below is the code I am attempting to use

    Code:
    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
            Dim strm As System.IO.Stream
            strm = OpenFileDialog1.OpenFile()
    
    
            'Dim strm As System.IO.Stream
    
    
            'strm = OpenFileDialog1.OpenFile()
    
    
            TextBox1.Text = OpenFileDialog1.FileName.ToString()
    
    
            If Not (strm Is Nothing) Then
    
    
                'insert code to read the file data
    
                Dim fileReader As String
                fileReader = My.Computer.FileSystem.ReadAllText(strm)
                MsgBox(fileReader)
    
    
                strm.Close()
    
    
                MessageBox.Show("file closed")
    
    
            End If
    
    
        End Sub
    I get an error saying

    "value of type "Stream" Cannot be converted to "String""

    I don't know how to correct this and what I am doing wrong. Ultimately I want to have the data go to TextBox1 .
    Thanks in Advance

    Dave

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Need help reading from a file

    It's good that you posted your code and the error message but you didn't tell us where the error occurred in that code. We can probably work it out ourselves but we shouldn't have to when you already know. In some cases, we may not be able to work it out so easily either.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Need help reading from a file

    As for the issue, it presumably occurs here:
    vb.net Code:
    1. fileReader = My.Computer.FileSystem.ReadAllText(strm)
    If you had paid attention to Intellisense when you typed that code or read the documentation for that method, you'd know that ReadAllText only accepts String containing a file path. You don't open the file first and then ask that method to read data from it. You just give it the path and it will open the file, read the contents and then close the file again. Basically, you should replace all that code with a single line:
    vb.net Code:
    1. TextBox1.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Need help reading from a file

    You should also turn Option Strict On, because it will warn you about issue like this at compile time instead of letting them slip through to run time. Do it in the project properties and also in the VS options, so it is On by default for future projects. It will force you to think about what data types you are and should be using and help you write better code as a result.

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2018
    Posts
    34

    Re: Need help reading from a file

    jmcilhinney,

    Thanks so much for your input, I have to confess, I copied the code from somewhere so I didn't type it myself, that's why Intellisense didn't alert me to it. I am very new at this if you couldn't tell and learning as I go, even though VB is supposed to be an easy language to learn, it's hard for an ole fart like me but I'm slowly learning.

    Thanks

    Dave

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2018
    Posts
    34

    Re: Need help reading from a file

    jmcilhinney,

    You were 100% correct as I expected. I also set Option Explicit in the project and in VS as you suggested.

    Thanks so much for your assistance

    Dave

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Need help reading from a file

    Quote Originally Posted by Dave14867 View Post
    I also set Option Explicit in the project and in VS as you suggested.
    Option Explicit is On by default. It controls whether or not you have to explicitly declare a variable before either getting or setting its value. There is NEVER a reason to turn Option Explicit Off.

    Option Strict is Off by default and you should turn it On in all existing projects and in the IDE options, so that it's On by default for future projects. It controls whether strict typing rules are enforced, so implicit narrowing conversions and late-binding are not permitted when it is On. The compiler will flag instances of either and the build will fail, meaning that mistakes like this one don't slip through to run time and potentially become harder to diagnose. In the rare cases where you need to use late-binding, you should still turn Option Strict On at the project level and then turn it Off at the file level in only those files that require it. You should also use partial classes to keep the code in files with Option Strict Off to an absolute minimum.

  8. #8

    Thread Starter
    Member
    Join Date
    Feb 2018
    Posts
    34

    Re: Need help reading from a file

    jmcilhinney,

    You are correct again, my apologies, I am used to Programming in Access and When I saw Option Explicit I was thinking it was what your were referring to, I didn't look at the next item down which is Option Strict. Thanks for bringing this dumb error to my attention again.

    I have it set in VS correctly now and also in the Project.

    Thanks

    Dave

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width