473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting FileInfo[]

Hi All,

..NET 1.1... I'm wondering if there is any approach more convenient to get a
list of FileInfo objects than the following. For example, if I wanted to get
1 list of all the Exe's and all the Dll's and all the Txt's in a folder, if
appears I need to do something like the following:

ArrayList InterestingFiles = new ArrayList();
DirectoryInfo di = new DirectoryInfo(@"C:\My Folder");

FileInfo[] EXEs = di.GetFiles("*.exe");
FileInfo[] DLLs = di.GetFiles("*.dll");
FileInfo[] TXTs = di.GetFiles("*.txt");

InterestingFiles.AddRange(EXEs);
InterestingFiles.AddRange(DLLs);
InterestingFiles.AddRange(TXTs);

foreach(FileInfo fi in InterestingFiles)
{
MessageBox.Show(fi.Name);
}

It would be really cool if the GetFiles method accepted an array of
searchPatterns to accomplish this in 1 line instead of 6... but alas I can
not find anything like the following.
eg,

string[] exts = {"*.exe", "*.dll", "*.txt"}
FileInfo[] AllInterestingFiles = di.GetFiles(exts);

This would be especially more convenient when the list of file extensions we
cared about was rather long. I suppose I could write a simple
FileInfoCollection class whose constructor accepted the array of exts and a
path, but that sort of seems like overkill. Anyone have any better ideas?

I realize that I probably should use a collection of FileInfo objects
instead of an array list for the real thing, but for this example purpose
I'm being lazy <g>.

TIA,

--
John C. Bowman
Software Engineer
Thermo Electron Scientific Instruments Div.
<Remove this before reply> jo*********@thermo.com
Nov 17 '05 #1
2 2493
John,

This would seem to be the best way. If you encapsulated it into a
function that has a params argument, you could actually do this:

public static FileInfo[] FindFiles(DirectoryInfo directory, params string[]
extensions)
{
// The values to return.
ArrayList retVal = new ArrayList();

// Cycle through the extensions, and add to a return value.
foreach (string extension in extensions)
{
// Find the files, and add to the return value.
retVal.AddRange(directory.GetFiles(extension));
}

// Return the array.
return (FileInfo[]) retVal.ToArray(typeof(FileInfo));
}

Then, you could just do:

foreach(FileInfo fi in FindFiles(new DirectoryInfo(@"C:\My Folder"),
"*.exe", "*.dll", "*.txt"))
{
MessageBox.Show(fi.Name);
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"John Bowman jo*********@thermo.com>" <<Remove this before reply> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
Hi All,

.NET 1.1... I'm wondering if there is any approach more convenient to get
a list of FileInfo objects than the following. For example, if I wanted to
get 1 list of all the Exe's and all the Dll's and all the Txt's in a
folder, if appears I need to do something like the following:

ArrayList InterestingFiles = new ArrayList();
DirectoryInfo di = new DirectoryInfo(@"C:\My Folder");

FileInfo[] EXEs = di.GetFiles("*.exe");
FileInfo[] DLLs = di.GetFiles("*.dll");
FileInfo[] TXTs = di.GetFiles("*.txt");

InterestingFiles.AddRange(EXEs);
InterestingFiles.AddRange(DLLs);
InterestingFiles.AddRange(TXTs);

foreach(FileInfo fi in InterestingFiles)
{
MessageBox.Show(fi.Name);
}

It would be really cool if the GetFiles method accepted an array of
searchPatterns to accomplish this in 1 line instead of 6... but alas I can
not find anything like the following.
eg,

string[] exts = {"*.exe", "*.dll", "*.txt"}
FileInfo[] AllInterestingFiles = di.GetFiles(exts);

This would be especially more convenient when the list of file extensions
we cared about was rather long. I suppose I could write a simple
FileInfoCollection class whose constructor accepted the array of exts and
a path, but that sort of seems like overkill. Anyone have any better
ideas?

I realize that I probably should use a collection of FileInfo objects
instead of an array list for the real thing, but for this example purpose
I'm being lazy <g>.

TIA,

--
John C. Bowman
Software Engineer
Thermo Electron Scientific Instruments Div.
<Remove this before reply> jo*********@thermo.com

Nov 17 '05 #2
John,
Anyone have any better ideas?


How about enumerating all files and then filter out the interesting
ones yourself?
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: PlanarIndia | last post by:
hi, I am facing an issue with getting the file size of a network shared file. I tried using FileInfo.length and it is returning me size that is rounded off to the nearest 1024 bytes. E.g. for...
2
by: Raed Sawalha | last post by:
I wondering if it possilble to get the directory files ordered by creation date string files = System.IO.Directory.GetFiles(strPath,"*.msg") can I sort the returned file by creation date? ...
2
by: Nony Buz | last post by:
My objective is simply: Notify a form when a image file has been created in a directory. First there is a basic interface for the callback function: public interface INewImageNotify { void...
5
by: Lance | last post by:
I want to expose properties of a class to a user via a PropertyGrid class. Some of the properties are of type System.IO.FileInfo. Ideally, an OpenFileDialog window would appear when the user...
5
by: mike | last post by:
Hi, I have been playing with VB.NET/C# for getting some general properties of a fileinfo object. However, FileInfo object does not seem to expose some of the basic properties like TYPE that used...
4
by: Paulers | last post by:
hello. how do I get the file's parent directory name out of a path like this? c:\test\myDirectory\file.txt Im looking to extract the "Directory" from the path so I can create myDirectory in...
3
by: rn5a | last post by:
To work with files, when should one use the File class & when should one use the FileInfo class? Similarly, to work with directories, when should one use the Directory class & when should one...
4
by: Robert Dufour | last post by:
In Vb.net 2003 I need to get all the names of any files that are in a particular folder, not in it's subfolders and put these in an array so that I can loop through it after getting the names. ...
0
by: comp.lang.php | last post by:
if (!function_exists('mime_content_type_fileinfo')) { /** * Will use {@link http://us2.php.net/fileinfo FileInfo} functions provided within {@link http://pecl.php.net PECL} bundle to return mime...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.