Introduction
If you need to determine the location of a specified file type in all directories of your computer then this article is very helpful for that.
Now first I determine all the directories of your system using a C# program.
Example
This example simply shows the drive names of all drives in the computer and also counts them.
using System;
using System.IO;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
int c=0;
DriveInfo[] TotalDrives = DriveInfo.GetDrives();
foreach (DriveInfo drvinfo in TotalDrives)
{
Console.WriteLine("Drive{0}", drvinfo.Name);
c++;
}
Console.WriteLine("Total no of Drives=" + c);
Console.ReadKey();
}
}
}
Output
The purpose of the code above is basically to use it in the following code, because when you want to search for a specified type of file in all drives of your computers, you need the name of each
drive of your computer in the method Directory.GetFiles(DirectoryName, "SpecifiedType", SearchOption.TopDirectoryOnly);.
Now I am writing code that displays all the specified types of a file name with the path, basically this code only returns the file name with path whose extension is "*.*". You can also change the specified type as needed.
Example
This example returns all the specified types of file name with path from all drives of your computer.
using System;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DriveInfo[] allDrive = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrive)
{
string b = d.Name.ToString();
var filename = Directory.GetFiles(b, "*.*", SearchOption.TopDirectoryOnly); // it returns the all file with the path whose type is "*.*".
foreach (string a in filename)
{
MessageBox.Show(a);
}
}
}
}
}
Output