Solved

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

Posted on 2004-08-20
10
266 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now