Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Iterating members of a FileInfo[] object 1

Status
Not open for further replies.

jasonp45

Programmer
Aug 23, 2001
212
US
I've instanced a FileInfo[] object like so:

Code:
FileInfo[] fileInfo = directoryInfo.GetFiles(fileNamePattern);

Then I want to do a switch statement depending upon how many filenames matching the pattern were found:

Code:
            switch (fileInfo.Count())
            {
                case 0: //No matches
                    return null;
                case 1: //Unique fileNamePattern match
                    this.FileNameMatchFound = true;
                    fileNameMatch = fileInfo[0].Name;
                    return fileNameMatch;
                default:
                    {
                        foreach (fileInfo...?)
                    }
            }

I'm having a problem figuring out the foreach portion for the default case. What I want to do is iterate all members of the fileInfo instance, and add the fileInfo.Names to a class property (probably a typed list). And I'd like to avoid having to create another FileInfo object if possible.

In googling I've come across several examples like
Code:
foreach (FileInfo f in dir.GetFiles())
...

but none for an already instanced FileInfo object.

Thanks!
 
foreach (FileInfo f in fileInfo)


you fileInfo is an array of fileinfos

BTW are you usnig .net 2008?

Christiaan Baes
Belgium

My Blog
 
Thanks. The logic's a little confusing for me as to when FileInfo objects are arrays though. If I instanced my initial fileInfo object as:

Code:
FileInfo fileInfo = directoryInfo.GetFiles(fileNamePattern);

would it work as a non-array? (I'll try it and see, but I'm curious as to when/if it needs to be an array of FileInfo's vs. not an array).

Yes, VS2008. Why?

I have a really annoying bug in VS2008...if I hit Ctrl+F to search for something (or Ctrl+H to find-replace), a fatal error occurs and the IDE closes and restarts, wiping out any unsaved changes. I've learned to save often and *not* search for anything!
 
In VS2008 you could use linq for these kinds of things.

getfiles will always return an array since it can return multiple files. FileInfo has the information about one file only.

Christiaan Baes
Belgium

My Blog
 
i don't understand the need for the switch statement.
Code:
this.FileNameMatchFound = false;
foreach(FileInfo file in directoryInfo.GetFiles(fileNamePattern))
{
   if(Regex.match(file.FullName, pattern))
   {
      this.FileNameMatchFound = true;
      return file.FullName;
   }
}
return string.Empty;

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
The switch is necessary because I want to handle 3 different cases: no match, unique match, multiple matches. The files I receive are generally named consistently, allowing for the pattern matching, but they might send a zip archive containing 5 files, and more than one might match the pattern, in which case I'll have to interact and ID the one containing the data I need.

I was initially going to try to use regex, but the GetFiles filter works fine for my purposes.

I'm not up on Linq yet as I'm learning C# from a 2005 book...it's on my list;).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top