Home All Groups Group Topic Archive Search About

Simple (I hope) SteamReader DirectoryInfo question

Author
30 Jan 2006 4:25 PM
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

Author
30 Jan 2006 4:34 PM
I Don't Like Spam
eric.gofo***@gmail.com wrote:
Show quoteHide quote
> 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
Author
30 Jan 2006 4:34 PM
eric.goforth
Never mind, this was just one of those goofy my app didn't recompile
things.

Thanks,
Eric