PDA

Click to See Complete Forum and Search --> : [RESOLVED] Finding the file-list of all files contained in a Folder


JenniferBabe
Mar 28th, 2006, 05:50 AM
Does anyone know how to find the list of files from a folder in a directory?

Jennifer

Shuja Ali
Mar 28th, 2006, 06:07 AM
An easy way would to use the DirectoryInfo class. //get the Folder
System.IO.DirectoryInfo myFolder = new System.IO.DirectoryInfo(@"C:\");
//loop through each file and delete
foreach (System.IO.FileInfo hFile in myFolder.GetFiles())
MesssageBox.Show(hFile.Name);


Edit--
Did I post VB.NET Code? :eek2:

jmcilhinney
Mar 28th, 2006, 06:25 AM
I see a lot of people recommend the use of the DirectoryInfo and FileInfo classes for this type of thing. While there is nothing really wrong with that, I'd consider it preferable to use the Directory and File classes. These two classes have all static members and thus do not require creation of an instance. Unless you need the extra functionality provided by the FileInfo class, like file size, I'd suggest going with the more efficient option.foreach (string filePath in Directory.GetFiles(@"C:\"))
{
MessageBox.Show(filePath)
}

Shuja Ali
Mar 28th, 2006, 06:30 AM
Agreed. :thumb:
I had this code handy as I have to do other stuff with the Files. So I just posted that with a small change.

JenniferBabe
Mar 28th, 2006, 06:42 AM
Thanks Guys, its worked fine!!!!! - jennifer