473,656 Members | 2,756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 InterestingFile s = new ArrayList();
DirectoryInfo di = new DirectoryInfo(@ "C:\My Folder");

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

InterestingFile s.AddRange(EXEs );
InterestingFile s.AddRange(DLLs );
InterestingFile s.AddRange(TXTs );

foreach(FileInf o fi in InterestingFile s)
{
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[] AllInterestingF iles = di.GetFiles(ext s);

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
FileInfoCollect ion 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*********@the rmo.com
Nov 17 '05 #1
2 2508
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(Direc toryInfo 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.GetF iles(extension) );
}

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

Then, you could just do:

foreach(FileInf o 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.co m
"John Bowman jo*********@the rmo.com>" <<Remove this before reply> wrote in
message news:%2******** ********@TK2MSF TNGP14.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 InterestingFile s = new ArrayList();
DirectoryInfo di = new DirectoryInfo(@ "C:\My Folder");

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

InterestingFile s.AddRange(EXEs );
InterestingFile s.AddRange(DLLs );
InterestingFile s.AddRange(TXTs );

foreach(FileInf o fi in InterestingFile s)
{
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[] AllInterestingF iles = di.GetFiles(ext s);

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
FileInfoCollect ion 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*********@the rmo.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
1459
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 a file of size 900 bytes, FileInfo.lenght returns me 1024. Using native GetFileSize also showed the same behaviour. Is there any way to find the correct size of a network shared file?
2
52748
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? or is there a ready method do the jobthat in .NET
2
2486
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 notify_NewImage(string imageName); } Then there is the delegate:
5
6531
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 attempted to edit the value of the System.IO.FileInfo properties. Is there an existing UITypeEditor that will do this type of thing, or will I need to create my own Thanks Lance
5
3796
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 to be available in "FileSystemObject" of ScriptingLibrary. For example the "TYPE" property should return "Text Document" or "Microsoft Office Document" or "Visual Studio Code File" and so on. similar to the properties displayed by Properties...
4
1208
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 another directory and copy the files. what I am really trying to accomplish is to move the parent directory
3
2160
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 use the DirectoryInfo class?
4
1594
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. There's a snippet in vb2005 but not in 2003 Any help appreciated greatly, Bob
0
1780
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 type * * @param string $file * @return string $mime_type * @see mime_content_type */
0
8382
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8498
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7311
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5629
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.