Simple (I hope) SteamReader DirectoryInfo question

E

eric.goforth

Hello,

When I step through the following code snippet:

Dim di As DirectoryInfo = New DirectoryInfo(strMyPath)
Dim sr As StreamReader

If di.Exists Then

Dim fi As FileInfo
For Each fi In di.GetFiles("*.txt")
sr = fi.OpenText()
Dim SomeText As String
SomeText = sr.ReadToEnd()
If sr.ReadToEnd().ToUpper.IndexOf("mytext") > 0
Then
MsgBox("success")
End If
Next fi

If I put a break point on the "SomeText = sr.ReadToEnd()" line and view
the value of sr.ReadToEnd it says "" (empty string). However, on the
same breakpoint fi.OpenText().ReadToEnd shows me "mytext". As you can
see, I set "sr = fi.OpenText()" a couple lines about the "SomeText =
sr.ReadToEnd()" line. Any idea what's going on?

Thanks,
Eric
 
G

Guest

Hello,

When I step through the following code snippet:

Dim di As DirectoryInfo = New DirectoryInfo(strMyPath)
Dim sr As StreamReader

If di.Exists Then

Dim fi As FileInfo
For Each fi In di.GetFiles("*.txt")
sr = fi.OpenText()
Dim SomeText As String
SomeText = sr.ReadToEnd()
If sr.ReadToEnd().ToUpper.IndexOf("mytext") > 0
Then
MsgBox("success")
End If
Next fi

If I put a break point on the "SomeText = sr.ReadToEnd()" line and view
the value of sr.ReadToEnd it says "" (empty string). However, on the
same breakpoint fi.OpenText().ReadToEnd shows me "mytext". As you can
see, I set "sr = fi.OpenText()" a couple lines about the "SomeText =
sr.ReadToEnd()" line. Any idea what's going on?

Thanks,
Eric

check what the length of the string is. There are some characters
("/0") that will make the string thing it's come to the end when you
display it in debug session.

You could do something like:

do while sr isnot nothing
sr = fi.readline
debug.writeline(sr)
loop

Also you can not do ReadToEnd twice. Once you readtoend, you are at the
end of the file and can not do a read again. Change your second read to:

If SomeText.ToUpper.IndexOf("mytext") > 0 Then

Why do you do a ToUpper but then check it against a lower case string?

Also, you may want to add a fi.Close before your "next fi" statement.


Chris
 
E

eric.goforth

Never mind, this was just one of those goofy my app didn't recompile
things.

Thanks,
Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top