|
-
Jan 27th, 2007, 04:20 PM
#1
Thread Starter
New Member
[RESOLVED] [2005] Reading Lines
I have a code and this is how I need it to work.
1. Download page from the internet into a string.
2. Search through lines
3. If a line includes a certain string, add it to a textbox
4. Until the end of the lines.
But mine doesnt work.
VB Code:
Private Sub btnGrab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrab.Click
Dim externalPage As String = Nothing
Dim Line As String
externalPage = WebClient1.DownloadString("http://iat.awardspace.com/text.txt")
Dim streamr As New IO.StreamReader(externalPage)
While Not streamr.EndOfStream
Line = streamr.ReadLine
If Line.Contains("S") Then
TextBox1.Text = TextBox1.Text & Line.ToString & vbNewLine
End If
End While
End Sub
Im fairly new to VB.NET and I know I've done something wrong. I think it's something to do with the StreamReader. Its supposed to read streams, but I dont have any. Can anybody help me get this working please?
-
Jan 27th, 2007, 04:32 PM
#2
Re: [2005] Reading Lines
Your code looks okay. Maybe try this as the Loop condition:
Do While sr.Peek <> -1
Loop
Alternatively, you could use this and avoid the streamreader code:
VB Code:
Dim myStr() As String
myStr = IO.File.ReadAllLines(externalPage)
For Each str As String In myStr
If str.Contains("text") Then
Me.TextBox1.Text = TextBox1.Text & str.ToString & vbNewLine
End If
Next
-
Jan 27th, 2007, 04:38 PM
#3
Thread Starter
New Member
Re: [2005] Reading Lines
Well both the StreamReader and ReadAllLines have to read files on your computer, not strings. Is there anything that could read strings?
-
Jan 27th, 2007, 05:04 PM
#4
Hyperactive Member
Re: [2005] Reading Lines
 Originally Posted by IAmThomas
Well both the StreamReader and ReadAllLines have to read files on your computer, not strings. Is there anything that could read strings?
I don't have .Net open but something like this should work:
VB Code:
Dim externalPage() As String = Split(WebClient1.DownloadString("http://iat.awardspace.com/text.txt"), VbCrLf)
For i As Integer = 0 To UBound(externalPage)
If externamePage(i).Contains("S") Then TextBox1.Text = TextBox1.Text & externamePage(i).ToString & vbNewLine
Next
God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.
I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P
-
Jan 27th, 2007, 05:15 PM
#5
Re: [2005] Reading Lines
But the string array that gets fed the text file would then be in string format would it not?
-
Jan 27th, 2007, 06:22 PM
#6
Hyperactive Member
Re: [2005] Reading Lines
 Originally Posted by stimbo
But the string array that gets fed the text file would then be in string format would it not?
Yes it would, but it's not like you couldn't convert it later if need be. Or better yet, convert it to a string to a separate variable so that you have both, and then follow the example I provided. That way, you can parse the string array as needed and you still have the stream.
God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.
I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P
-
Jan 28th, 2007, 01:45 PM
#7
Thread Starter
New Member
Re: [2005] Reading Lines
Im having problems. The code isn't splitting properly. I dont think its the right character that it is being split by.
-
Jan 28th, 2007, 01:54 PM
#8
Hyperactive Member
Re: [2005] Reading Lines
 Originally Posted by IAmThomas
Im having problems. The code isn't splitting properly. I dont think its the right character that it is being split by.
It's splitting the file by lines. If your file is multiline, then it should be splitting it just fine. If not, insert a break point somewhere or debug.print in there to see how it is breaking things up and post it here.
God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.
I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P
-
Jan 28th, 2007, 02:00 PM
#9
Thread Starter
New Member
Re: [2005] Reading Lines
This is my file:
http://iat.awardspace.com/text.txt
This is my code:
VB Code:
Private Sub btnGrab_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrab.Click
Dim externalPage() = Split(WebClient1.DownloadString("http://iat.awardspace.com/text.txt"), vbCrLf)
For i As Integer = 0 To UBound(externalPage)
If externalPage(i).Contains("S") Then TextBox1.Text = TextBox1.Text & externalPage(i).ToString & vbNewLine
Next
End Sub
And this is what happens when I execute it:
-
Jan 28th, 2007, 02:41 PM
#10
Re: [2005] Reading Lines
If you had put a messagebox after the split command you would realise that it's not splitting the file by vbCrLf.
It should be this:
VB Code:
Dim externalPage() As String = webClient1.DownloadString("http://iat.awardspace.com/text.txt").Split(Chr(10))
Last edited by stimbo; Jan 28th, 2007 at 02:50 PM.
-
Jan 28th, 2007, 02:48 PM
#11
Thread Starter
New Member
Re: [2005] Reading Lines
Thanks, I was trying to find which Chr it was but I didnt try 10
Thanks.
-
Jan 28th, 2007, 03:03 PM
#12
Hyperactive Member
Re: [2005] Reading Lines
 Originally Posted by IAmThomas
Thanks, I was trying to find which Chr it was but I didnt try 10
Thanks.
You should always know what you're working with. I assumed that each line was split by a standard carriage return, because 9 times out of 10 they are. In any case, the code should work fine now if you replace the VbCrLf with Chr$(10)
God put me on this earth to do many great things, and I'm so far behind that I'm going to live forever.
I'm programming for Windows using a Apple Mac Mini, 1.5Ghz with 512MB DDR RAM. I feel like I'm committing a crime :P
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
|