Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-08-20
10
Medium Priority
?
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 10

Accepted Solution

by:
eternal_21 earned 150 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

618 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