Results 1 to 16 of 16

Thread: [RESOLVED] [2008] Need help with StreamReader

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [RESOLVED] [2008] Need help with StreamReader

    I'm trying to read a *.txt file and see if it has anything written on it. This is my code:

    Code:
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
                Dim line As String = Nothing
                Dim reader As New IO.StreamReader("C:\text.txt")
                line = reader.ReadLine()
                If line = Nothing Then
                    MsgBox("There are no text!", MsgBoxStyle.Exclamation, "Error")
                End If
    End Sub
    But when i use it doesn't pop the MsgBox. What's wrong with it?
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008] Need help with StreamReader

    Just a guess here but it might be that the ReadLine() method returns an empty string (String.Empty, or "") instead of Nothing when there is no text. So you should check if line = String.Empty or line = "" (one of these two, not both).

    In your current code, line is never Nothing. It is either an empty string or a non-empty string (the text in the file).

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Need help with StreamReader

    I tried that, still nothing happens
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008] Need help with StreamReader

    Worked just fine here... Are you sure your txt file is empty? It doesn't contain a space or some invisible character?

    I also tried it like this, and it works fine:
    vb.net Code:
    1. Dim txt As String
    2.         Dim r As New IO.StreamReader("C:\text.txt")
    3.         txt = r.ReadToEnd()
    4.  
    5.         If txt = String.Empty Then
    6.             MsgBox("No text...")
    7.         End If
    8.  
    9.         r.Close()

    Also, don't forget to Close your StreamReader object otherwise your file will remain locked!

  5. #5
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Need help with StreamReader

    What, exactly, does 'line' equal to when it reaches the IF-statement?
    And more importantly, what does the textfile contain?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Need help with StreamReader

    If .Peek() > 0, then there's something in there and you can do a ReadLine.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Need help with StreamReader

    @Atheist:

    The text file contains a bunch of names, that are the items of a listbox. I just want check it, to see if its empty or if it has names in it.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Need help with StreamReader

    What the frog is saying is that there's no need to actually read anything if all you want to know is whether there is data or not. You simply open the StreamReader and call Peek to see whether there's data or not. The EndOfStream property will do the same thing.

    That said, there's no point even opening the file if all you want to know is whether it contains data or not.
    vb.net Code:
    1. If New IO.FileInfo("file path here").Length > 0 Then
    2.     'There is data.
    3. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Need help with StreamReader

    Doesn't work for me. Is there a way to find out if a listbox has no items? I tried:

    Code:
    If ListBox.Items.Count = 0 Then
    'Something
    End If
    But that doesn't work, because the starting index = 0. So, is there any other ways?

    EDIT: Nvm, got it working.
    Last edited by tassa; Jan 19th, 2009 at 11:56 PM.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Need help with StreamReader

    Quote Originally Posted by tassa
    Doesn't work for me.
    We're not pulling your leg here. We're making these suggestions because we know they work. No offence but, if they don't work for you, it's because you're implementing them wrong. If you don't show us what you tried then we can't tell you what you did wrong.
    Quote Originally Posted by tassa
    Is there a way to find out if a listbox has no items? I tried:

    Code:
    If ListBox.Items.Count = 0 Then
    'Something
    End If
    But that doesn't work, because the starting index = 0. So, is there any other ways?
    That IS the way and it DOES work. The index of the last item is always 1 less than the Count. If there was 1 item its index would be 0 and the Count would be 1. If the Count is 0 then there are no items. This supports the theory that you just didn't implement the other ideas we suggested properly. I'm telling you for a fact that if you do this:
    vb.net Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.             If New IO.FileInfo("C:\text.txt").Length = 0 Then
    3.                 MsgBox("There is no text!", MsgBoxStyle.Exclamation, "Error")
    4.             End If
    5. End Sub
    then it will do what you've told us you want to do. If it doesn't do what you ACTUALLY want to do then you have misinformed us.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [2008] Need help with StreamReader

    That's why i edited the post, because it worked for me. I didn't post another one to prevent double post or spam. But it worked!

    So... Thanks!
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Need help with StreamReader

    Quote Originally Posted by tassa
    That's why i edited the post, because it worked for me. I didn't post another one to prevent double post or spam. But it worked!

    So... Thanks!
    It wouldn't have been a double post or spam to post new information. It's generally not a good idea to edit existing posts unless it's to correct something, which I guess that this case could be construed as. I was already viewing your post before the edit so I didn't see it. Anyway, all's well that ends well.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [RESOLVED] [2008] Need help with StreamReader

    I used that code. Thanks everyone for your help!
    Code:
    If New IO.FileInfo(My.Settings.CurrentDirectory & "List.txt").Length = 0 Then
                If ListBox1.Items.Count >= 1 Then
                    Dim printFont As New System.Drawing.Font("Tahoma", 12, System.Drawing.FontStyle.Regular)
                    Dim writer As New IO.StreamWriter(My.Settings.CurrentDirectory & "List.txt")
                    For i As Integer = 0 To ListBox1.Items.Count - 1
                        writer.WriteLine(ListBox1.Items.Item(i))
                    Next
                    writer.Close()
                    statusClose = False
                    Me.Close()
                Else
                    MsgBox("There is no text!", MsgBoxStyle.Exclamation, "Error!")
                End If
            End If
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] [2008] Need help with StreamReader

    There are a few minor improvements I'd make to that:
    vb.net Code:
    1. Dim filePath As String = IO.Path.Combine(My.Settings.CurrentDirectory, "List.txt")
    2.  
    3. If New IO.FileInfo(filePath).Length = 0 Then
    4.     If ListBox1.Items.Count = 0 Then
    5.         MessageBox.Show("There is no text!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    6.     Else
    7.         Using writer As New IO.StreamWriter(filePath)
    8.             For Each item As String In ListBox1.Items
    9.                 writer.WriteLine(item)
    10.             Next
    11.         End Using
    12.  
    13.         statusClose = False
    14.         Me.Close()
    15.     End If
    16. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: [RESOLVED] [2008] Need help with StreamReader

    What is the Path.Combine for?

    Edit: I just understood how it works. Thanks for the improvement!
    Last edited by tassa; Jan 20th, 2009 at 12:25 AM.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] [2008] Need help with StreamReader

    Quote Originally Posted by tassa
    What is the Path.Combine for?
    This is the best way to learn. If you see a type or member you're not familiar with you go immediately to the documentation for that type or member and you read about it. I cannot tell you how many times that has provided me with the answer to my question and more besides.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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