Get files in Sub Directory and further subdirectories

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi Everyone,<br/><br/>can someone help me?<br/><br/>Im trying to (at the moment) list all files with the extension.txt in a directory and all subdirectories under the root directory.<br/><br/>Ill give a directory example<br/><br/>                                Topfolder<br/>     File1.txt       Folder1             Folder2            Folder3<br/>                     <br/>                       File2.txt             File3.txt           File5.txt<br/>                                               File4.txt<br/><br/>The code i have so far is like this;<br/><br/>
<pre lang="x-c# class FilesFolders
{
private DirectoryInfo dirInfo;
private string LocalFolderPath = "";

#region Initializers


private FilesFolders() { }

public FilesFolders(string folderPath)
{
try
{
LocalFolderPath = folderPath;
dirInfo = new DirectoryInfo(folderPath);

if (!dirInfo.Exists)
{
throw new DirectoryNotFoundException(); // ***** Add a custom Exception *****
}
}
catch (Exception e)
{
Console.WriteLine("Error:");
Console.WriteLine(e.Message);
}

}

#endregion

public ArrayList GetInfo()
{
ArrayList DirectoryInformation = new ArrayList();

DirectoryInfo[] dirs = dirInfo.GetDirectories();
FileInfo[] files = dirInfo.GetFiles();
FileInfo[] subFiles;

foreach (DirectoryInfo dir in dirs)
{
DirectoryInformation.Add(dir.Name);
/*
Need to go folder levels deeper here
*/
}

foreach (FileInfo file in files)
{
if (file.Extension == ".txt")
{
DirectoryInformation.Add(file.Name);
}
}


return DirectoryInformation;
}

public void ReturnFiles(ArrayList list)
{
foreach (object obj in list)
{
Console.WriteLine((string)obj);
}
}
}[/code]
<br/>Im really not sure where to go now!!!<br/><br/>Thanks in Advance<br/>Dale Turley

View the full article
 
Back
Top