FileInfo[] Array - How the heck do I add to it?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thomasbihn
    New Member
    • Mar 2010
    • 6

    FileInfo[] Array - How the heck do I add to it?

    I have a pretty urgent application, but want to try and do it in C# instead of VB6. I need to search subfolders of a selected folder. Inside these subfolders, I want to return the files that have the pattern "*_LOG_*.tx t". I keep running into an issue though. I try to declare an array of FileInfo[] type, but the declaration insists I use a constructor that doesn't take 0 arguements. I got around this by placing in a dummy text string "foo.txt". When I attempt to add to the array using the following statement, I get an error:

    currentSubDir.G etFiles("*_LOG_ *txt").CopyTo(r eturnArray, returnArray.Cou nt());

    I thought that if I specify the count, it would add to the end of the array. Can someone help me get unstuck?

    The error I get is Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
  • RustyDoorknobs
    New Member
    • Jun 2010
    • 2

    #2
    Originally posted by thomasbihn
    I have a pretty urgent application, but want to try and do it in C# instead of VB6. I need to search subfolders of a selected folder. Inside these subfolders, I want to return the files that have the pattern "*_LOG_*.tx t". I keep running into an issue though. I try to declare an array of FileInfo[] type, but the declaration insists I use a constructor that doesn't take 0 arguements. I got around this by placing in a dummy text string "foo.txt". When I attempt to add to the array using the following statement, I get an error:

    currentSubDir.G etFiles("*_LOG_ *txt").CopyTo(r eturnArray, returnArray.Cou nt());

    I thought that if I specify the count, it would add to the end of the array. Can someone help me get unstuck?

    The error I get is Destination array was not long enough. Check destIndex and length, and the array's lower bounds.
    I think you are supposed to use Lists instead of Arrays, google it.

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Code:
      List<FileInfo> fileList = currentSubDir.GetFiles("*_LOG_*txt").ToList();
      Alternatively:
      Code:
      FileInfo[] fileArray = currentSubDir.GetFiles("*_LOG_*txt");
      I just prefer using Lists to arrays because they're easier to work with (adding and removing, that kind of thing).

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Also:

        I try to declare an array of FileInfo[] type, but the declaration insists I use a constructor that doesn't take 0 arguements.
        How are you declaring your array? The proper way to declare an array is:
        Code:
        FileInfo[] fileArray = new FileInfo[5];
        Replace 5 with the proper size.

        Comment

        • thomasbihn
          New Member
          • Mar 2010
          • 6

          #5
          Thanks for all the tips. I could've done this app in just a few minutes in VB6, but am an absolute idiot as I try to make the paradigm shift to C#. The problem is that in manufacturing, there is little luxury of time to find a suitable app to slowly develop in C#, so I've decided even when the requirement is urgent, I'm going to try to struggle through doing it in C# :)

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            No prob. Trust me, it's worth learning. C# is an elegant language compared to any kind of VB, but especially VB6. It's easier if you've had some Java background, but once you get used to it, you'll love it.

            Comment

            Working...