Find All Text files in all directories.

Here is the sample code for find all text file in all directories.

private void Form1_Load(object sender, EventArgs e)
{
foreach (string drives in Environment.GetLogicalDrives()) // Getting Logical drivers
{
FindFiles(drives);
}
}


private void FindFiles(string drives)
{
try
{
string[] directory = Directory.GetDirectories(drives); // Getting directories from a Directory
if (directory.Length > 0)
{
for (int i = 0; i < directory.Length; i++)
{
try
{
string[] files = Directory.GetFiles(directory[i], "*.txt"); //Finding Text files only
if (files.Length > 0)
{
for (int j = 0; j < files.Length; j++)
{
try
{
lstAllFiles.Items.Add(files[j]); //Adding files in to a list box
}
catch { }
}
}
}
catch { }
FindFiles(directory[i]);
}
}
}
catch { }
}

No comments: