|
-
Jan 19th, 2009, 11:42 AM
#1
Thread Starter
Fanatic Member
[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?
-
Jan 19th, 2009, 11:45 AM
#2
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).
-
Jan 19th, 2009, 11:49 AM
#3
Thread Starter
Fanatic Member
Re: [2008] Need help with StreamReader
I tried that, still nothing happens
-
Jan 19th, 2009, 11:54 AM
#4
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:
Dim txt As String Dim r As New IO.StreamReader("C:\text.txt") txt = r.ReadToEnd() If txt = String.Empty Then MsgBox("No text...") End If r.Close()
Also, don't forget to Close your StreamReader object otherwise your file will remain locked!
-
Jan 19th, 2009, 11:54 AM
#5
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?
-
Jan 19th, 2009, 11:59 AM
#6
Re: [2008] Need help with StreamReader
If .Peek() > 0, then there's something in there and you can do a ReadLine.
-
Jan 19th, 2009, 11:20 PM
#7
Thread Starter
Fanatic Member
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.
-
Jan 19th, 2009, 11:26 PM
#8
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:
If New IO.FileInfo("file path here").Length > 0 Then 'There is data. End If
-
Jan 19th, 2009, 11:51 PM
#9
Thread Starter
Fanatic Member
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.
-
Jan 19th, 2009, 11:59 PM
#10
Re: [2008] Need help with StreamReader
 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.
 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:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If New IO.FileInfo("C:\text.txt").Length = 0 Then
MsgBox("There is no text!", MsgBoxStyle.Exclamation, "Error")
End If
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.
-
Jan 20th, 2009, 12:02 AM
#11
Thread Starter
Fanatic Member
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!
-
Jan 20th, 2009, 12:04 AM
#12
Re: [2008] Need help with StreamReader
 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.
-
Jan 20th, 2009, 12:11 AM
#13
Thread Starter
Fanatic Member
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
-
Jan 20th, 2009, 12:18 AM
#14
Re: [RESOLVED] [2008] Need help with StreamReader
There are a few minor improvements I'd make to that:
vb.net Code:
Dim filePath As String = IO.Path.Combine(My.Settings.CurrentDirectory, "List.txt") If New IO.FileInfo(filePath).Length = 0 Then If ListBox1.Items.Count = 0 Then MessageBox.Show("There is no text!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) Else Using writer As New IO.StreamWriter(filePath) For Each item As String In ListBox1.Items writer.WriteLine(item) Next End Using statusClose = False Me.Close() End If End If
-
Jan 20th, 2009, 12:21 AM
#15
Thread Starter
Fanatic Member
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.
-
Jan 20th, 2009, 12:25 AM
#16
Re: [RESOLVED] [2008] Need help with StreamReader
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|