Solved

Quick question: Size limitation in Directory.GetFiles or string []?

Posted on 2004-08-20
10
269 Views
Last Modified: 2012-05-05
I am having a problem getting the contents of a directory with 479 files. The code is as follows:

____________________________________________________________________
strFiles = Directory.GetFiles ("target");

      for (int counter = 0; counter < strFiles.Length; counter++)
      {
            if (strFiles [counter].EndsWith (".jpg") || strFiles [counter].EndsWith (".jpeg"))
            {
                  lvwFiles.Items [counter].Tag = strFiles [counter];
            }

      }
____________________________________________________________________

The error I get is:
"An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll

"Additional information: Specified argument was out of the range of valid values."

The error occurs when counter is 379. Is there a size limitation to the string [] or the Directory.GetFiles () function that I don't know about? If not any suggestions as to why I am getting the problem?

Thank you in advance...
Taarik.
0
Comment
Question by:TZRick
  • 5
  • 5
10 Comments
 
LVL 10

Accepted Solution

by:
eternal_21 earned 50 total points
ID: 11852546
My first guess is that the problem lies here: lvwFiles.Items [counter].  Are there the same number of items in your ListView as there are entries in strFiles?
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11852567
Can you post the code where the ListView is populated?
0
 
LVL 1

Author Comment

by:TZRick
ID: 11852582
Yes...I deleted some of the code for clarity's sake. I'll repost the code once more...

____________________________________________________________________
//ListView lvwFiles = new ListView;
strFiles = Directory.GetFiles ("target");

     for (int counter = 0; counter < strFiles.Length; counter++)
     {
          if (strFiles [counter].EndsWith (".jpg") || strFiles [counter].EndsWith (".jpeg"))
          {
               lvwFiles.Items.Add (strFiles [counter]);
               lvwFiles.Items [counter].Tag = strFiles [counter];
          }

     }
____________________________________________________________________
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:TZRick
ID: 11852626
Check this site out...

http://www.dotnet247.com/247reference/msgs/22/110482.aspx

"...you just need to keep an eye
on the memory foot-print when doing this, and also
the possibility of causing a stack overflow - although
I think you would run into a limitation of NTFS before
you blew the stack in .NET :)"
0
 
LVL 1

Author Comment

by:TZRick
ID: 11852631
Did I "blow the stack"?
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11852711
Try this:

  //ListView lvwFiles = new ListView;
  strFiles = Directory.GetFiles ("target");

  foreach(string strFile in strFiles)
    if(strFile.EndsWith (".jpg") || strFile.EndsWith (".jpeg"))
    {
      ListViewItem lviJpeg = new ListViewItem();
      lviJpeg.Text = strFile;
      lviJpeg.Tag = strFile;
      lvwFiles.Items.Add(lviJpeg);
    }

And let me know what happens.  Also, you shouldn't need to use the Tag property since the Text is the main description of the ListViewItem (and must be unique).
0
 
LVL 1

Author Comment

by:TZRick
ID: 11852730
Sorry...I didn't see your last post until after I accepted.
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11852740
No, if you blew the stack you would get a StackOverflowException, and that is only referring to recursion (i.e.: You were collecting names of JPEGs from the entire hard drive through a recursive search).
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 11852746
Oh, did you fix it?
0
 
LVL 1

Author Comment

by:TZRick
ID: 11872494
Thanks so much! The problem has been fixed! It was a ridiculous logic error. strFiles and lvwFiles.Items had different indices, because strFiles contained all filenames, whereas lvwFiles.Items was supposed to have only JPEGs.

Thank you once again!

Taarik.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question