[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2004-08-20
10
Medium Priority
?
275 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 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
Technology Partners: 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

825 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