Searching an entire hard drive for a file
-
Anyone have the code worked out to scan every file in every directory on a hard drive to locate a single file possibly using DirectoryInfo and FileInfo? Kyle
You could do something like this, inside the loop I am adding the file to a listbox:
string m_CurrentDirectory = "C:\";
string[] AllFiles = Directory.GetFiles(m_CurrentDirectory, "*.*");
foreach (string aFile in AllFiles)
{
string aFileName = GetFileNameFromPath(aFile);
this.listBox1.Items.Add(aFileName);
} -
You could do something like this, inside the loop I am adding the file to a listbox:
string m_CurrentDirectory = "C:\";
string[] AllFiles = Directory.GetFiles(m_CurrentDirectory, "*.*");
foreach (string aFile in AllFiles)
{
string aFileName = GetFileNameFromPath(aFile);
this.listBox1.Items.Add(aFileName);
} -
You could do something like this, inside the loop I am adding the file to a listbox:
string m_CurrentDirectory = "C:\";
string[] AllFiles = Directory.GetFiles(m_CurrentDirectory, "*.*");
foreach (string aFile in AllFiles)
{
string aFileName = GetFileNameFromPath(aFile);
this.listBox1.Items.Add(aFileName);
}public void SearchHardDrive(string harddrive)
{
DirectoryInfo di = new DirectoryInfo(harddrive);SearchDirectory(di);
}private void SearchDirectory(DirectoryInfo di)
{
DirectoryInfo [] dirs = di.GetDirectories();
foreach(DirectoryInfo dir in dirs)
{
SearchDirectory(dir);
}FileInfo [] files = di.GetFiles();
foreach(FileInfo file in files)
{
DoSomethingWithFileInfo(file);
}
}private void DoSomethingWithFileInfo(FileInfo file)
{}
Enjoy :) [Edit: Okay, I'm an idiot I click on the wrong post to reply too ;P] James Simplicity Rules!
-
public void SearchHardDrive(string harddrive)
{
DirectoryInfo di = new DirectoryInfo(harddrive);SearchDirectory(di);
}private void SearchDirectory(DirectoryInfo di)
{
DirectoryInfo [] dirs = di.GetDirectories();
foreach(DirectoryInfo dir in dirs)
{
SearchDirectory(dir);
}FileInfo [] files = di.GetFiles();
foreach(FileInfo file in files)
{
DoSomethingWithFileInfo(file);
}
}private void DoSomethingWithFileInfo(FileInfo file)
{}
Enjoy :) [Edit: Okay, I'm an idiot I click on the wrong post to reply too ;P] James Simplicity Rules!
James T. Johnson wrote: [Edit: Okay, I'm an idiot I click on the wrong post to reply too ] No way James, I didn't have time to reply to Kyles response yet. Thanks Nick Parker
-
James T. Johnson wrote: [Edit: Okay, I'm an idiot I click on the wrong post to reply too ] No way James, I didn't have time to reply to Kyles response yet. Thanks Nick Parker
Thanks guys, Here is an approach that works well. Kyle using System.Management; string drive = "\"c:\""; string filename = "\"test\"" ; string extension = "\"exe\""; string qry = String.Format( "select * from cim_logicalfile where drive={0} and filename={1} and extension={2}", drive, filename, extension); ManagementObjectSearcher query = new ManagementObjectSearcher(qry); ManagementObjectCollection queryCollection = query.Get(); foreach(ManagementObject mo in queryCollection) { Console.WriteLine( "Name '{0}' :Path: '{1}' ",mo["Name"], mo["Path"]); } [Edit: The : P shows up as a smily face. Although I am happy with this result the smily face was unintentional. ]