473,466 Members | 4,855 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Simple (I hope) SteamReader DirectoryInfo question

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

Jan 30 '06 #1
2 1922
Never mind, this was just one of those goofy my app didn't recompile
things.

Thanks,
Eric

Jan 30 '06 #2
er**********@gmail.com wrote:
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
Jan 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jacek Dziedzic | last post by:
Hello! First of all please forgive me for not posting a compilable snippet, but rather a simplified piece of code with the unimportant details left out. Let's say I have two classes...
7
by: dan | last post by:
hey peeps, i am completely new at c++ and i need some help with an assignment. it is basically about file i/o with fstreams. i understand how to open a file with fstream, but how would you read,...
28
by: Andy | last post by:
Hi, I have a homework question, can some expert help me with this. q.Write a program that reads a text file and reports the number of integers and you may assume a number is defined as one or...
5
by: Will | last post by:
I've got a simple form that collects data from a user and I want to display that data in a sortable grid or table. The nature of this data is rather temporary, so there's no need to persist it to a...
5
by: Burak | last post by:
Hello, I would like to format the string "11304200" into "11-3042.00". Can I do this with String.Format method? I have not come across any good documentation. Thank you,
27
by: karan.shashi | last post by:
Hey all, I was asked this question in an interview recently: Suppose you have the method signature bool MyPairSum(int array, int sum) the array has all unique values (no repeats), your...
4
by: SM | last post by:
Hello, I have a simple question, but can't find the answer. I have a string that contains a path to a file I want to add another string to the end of that string So, if i have : path =...
1
by: Johnny Jörgensen | last post by:
I do a lot of winforms programming in C# and VB.NET, but not I've been asked to do an ASP.NET application, and that's a first for me. So I hope you will bear with this basic question, because I'm...
3
by: csharpula csharp | last post by:
Hello, I got an object of DirectoryInfo and I would like to know how can I get the names of all child directories of this folder? Thank you! *** Sent via Developersdex...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.