PC Review


Reply
Thread Tools Rate Thread

DirectoryInfo.GetFiles

 
 
Paul K
Guest
Posts: n/a
 
      31st Jul 2003
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
 
Reply With Quote
 
 
 
 
Gabriele G. Ponti
Guest
Posts: n/a
 
      31st Jul 2003
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 K" <(E-Mail Removed)> wrote in message
news:009b01c35790$6d807c70$(E-Mail Removed)...
> 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



 
Reply With Quote
 
Gabriele G. Ponti
Guest
Posts: n/a
 
      31st Jul 2003
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

"Paul K" <(E-Mail Removed)> wrote in message
news:019d01c3579b$66ad0bb0$(E-Mail Removed)...
> Gabriele,
>
> 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);
> }
>
> Paul
>
>
> >-----Original Message-----
> >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 K" <(E-Mail Removed)> wrote in message
> >news:009b01c35790$6d807c70$(E-Mail Removed)...
> >> 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

> >
> >
> >.
> >



 
Reply With Quote
 
Paul K
Guest
Posts: n/a
 
      31st Jul 2003
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
>-----Original Message-----
>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
>
>"Paul K" <(E-Mail Removed)> wrote in message
>news:019d01c3579b$66ad0bb0$(E-Mail Removed)...
>> Gabriele,
>>
>> 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);
>> }
>>
>> Paul
>>
>>
>> >-----Original Message-----
>> >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 K" <(E-Mail Removed)> wrote in message
>> >news:009b01c35790$6d807c70$(E-Mail Removed)...
>> >> 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
>> >
>> >
>> >.
>> >

>
>
>.
>

 
Reply With Quote
 
Jon Skeet
Guest
Posts: n/a
 
      31st Jul 2003
Paul K <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
 
Reply With Quote
 
Paul K
Guest
Posts: n/a
 
      31st Jul 2003
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
>-----Original Message-----
>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
>
>"Paul K" <(E-Mail Removed)> wrote in message
>news:019d01c3579b$66ad0bb0$(E-Mail Removed)...
>> Gabriele,
>>
>> 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);
>> }
>>
>> Paul
>>
>>
>> >-----Original Message-----
>> >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 K" <(E-Mail Removed)> wrote in message
>> >news:009b01c35790$6d807c70$(E-Mail Removed)...
>> >> 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
>> >
>> >
>> >.
>> >

>
>
>.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Q: DirectoryInfo.GetFiles Martin Arvidsson, Visual Systems AB Microsoft C# .NET 1 17th Apr 2007 04:07 PM
Directory.GetFiles() / DirectoryInfo.GetFiles() on a remote server =?Utf-8?B?RGFuaWVsIENvcnLDqmE=?= Microsoft ASP .NET 2 24th Mar 2007 06:59 PM
DirectoryInfo.GetFiles -> from all subfolders Mrozu Microsoft VB .NET 6 7th Apr 2006 07:37 PM
DirectoryInfo.GetFiles CJ Taylor Microsoft VB .NET 5 4th Nov 2003 05:27 PM
DirectoryInfo.GetFiles() Pluto Microsoft C# .NET 4 9th Oct 2003 03:49 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:19 PM.