Solved

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

Posted on 2004-08-20
10
271 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 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
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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