DirectoryInfo.GetFiles

  • Thread starter Thread starter Paul K
  • Start date Start date
P

Paul K

I seem to be running into a problem with the
DirectoryInfo.GetFiles method. If I use this method with
a search pattern on a few files there is no problem, but
as soon as the number of files grows somewhat large, it
seems to lock up. For example, I iterate through the
array returned by the GetFiles method and test for the
presence of a string in each file. Here's some code:

StreamReader sr;

foreach (FileInfo fi in di.GetFiles("*.PSM"))
{
sr=fi.OpenText();
..
..
..
}

With a few files (maybe 10 - 15) there is no problem, but
when I test it with more (around 50 - 70), it just seems
to lock. I doubt (but I could be wrong) that there is a
problem with the DirectoryInfo class regarding larger
numbers of files, so does anyone have any idea what I am
missing or doing incorrectly (or a better alternative)?

Paul
 
Paul,

For what is worth, I don't have the problem you described. Is it true that
with more files it takes a little longer, but that's normal. Running a
second time the program in a folder with about 300 files matching the
pattern it only took a few seconds (I guess the disk cache is kicking in).

What is your program actually doing inside the foreach{} statement?

Gabriele
 
Paul,

Does your code actually locks, or it just takes long time? I don't see
anything wrong with your code, but it could be slightly optimized. Is this
what you are looking for?

Gabriele
 
Gabriele,

I think that there's something I'm missing or not
doing in the do loop. I just ran a test with only a few
files that match the pattern, and it locked up again.
I'm using a compiled version of the app, so when I get
home tonight, I'll take a look at it in the debugger and
see where the problem is. Thanks!

Paul
 
Paul K said:
Here's the code for the foreach statement:

foreach (FileInfo fi in di.GetFiles("*.PSM"))
{
sr=fi.OpenText();
do
{
currentline=sr.ReadLine()

if (currentline!=null)
{
if (currentline.IndexOf(0,dbname) != -1)
{
found=true;
mlgname=fi.Name
}
}
}
while (!found || currentline!=null);
}

One thing you should try is calling Close or Dispose on each
StreamReader when you're finished with it. The easiest way to do this
is with the using construct:

foreach (FileInfo fi in di.GetFiles("*.PSM"))
{
using (StreamReader sr = fi.OpenText())
{
do
{
currentline=sr.ReadLine()

if (currentline!=null)
{
if (currentline.IndexOf(0,dbname) != -1)
{
found=true;
mlgname=fi.Name
}
}
}
while (!found || currentline!=null);
}
}

Otherwise you've got all the files open at the same time until garbage
collection kicks in.
 
HA! Found the problem:

while (!found || currentline!=null)

should be

while (!found && currentline!=null)

I realized that the problem was occurring only when the
string I was searching for was not found in the first
file to be searched. In that case, found would
perpetually be null. Now, if either the string is found
or the end of the stream is reached, it will break out of
the do loop.

Paul
 
Back
Top