TheDarklord
February 5th, 2010, 04:50 PM
I have a sample which I rewrote so that it will only search for mp3 extension files.
It allows the user to select a drive and then one folder.
What would be the best to edit it so that it allows the user to search through a specific directory path?
namespace FindFiles
{
public partial class Form1 : Form
{
#region private void AddComboBoxItem
private void AddComboBoxItem(String[] strDrives)
{
for (int i = 0; i < strDrives.Length; i++)
{
cmbDrives.Items.Add(strDrives[i]);
}
}
#endregion
#region private void AddListBoxItem
private void AddListBoxItem()
{
listBox1.Items.Add(cmbDrives.Text);
}
#endregion
#region private String[] GetDrives
private String[] GetDrives()
{
return Directory.GetLogicalDrives();
}
#endregion
#region private Directory[] GetDirectories
private DirectoryInfo[] GetDirectories(String strDrive)
{
DirectoryInfo dir = new DirectoryInfo(strDrive);
DirectoryInfo[] childDirs = dir.GetDirectories();
return childDirs;
}
#endregion
#region public Form1
public Form1()
{
InitializeComponent();
String[] strDrives = GetDrives();
AddComboBoxItem(strDrives);
// Display the form as a modal dialog box.
ShowDialog();
}
#endregion
#region protected void button1_Click
protected void button1_Click(object sender, System.EventArgs e)
{
String strMessage = "No Matches";
listBox1.Items.Clear();
if (cmbDrives.Text == "")
{
lblHeader.Text = "Select the drive and Enter the file type to be searched!";
}
else if (cmbDrives.Text == "")
{
lblHeader.Text = "Select the drive to search the files!";
}
else
{
try
{
int i = 0;
//int iVal;
DirectoryInfo[] ChildDirs = this.GetDirectories(cmbDrives.Text);
String txtSearchType = "*.mp3";
//If the Search file type has no extention then add one.
//Get the Child Directories.
foreach (DirectoryInfo ChildDir in ChildDirs)
{
if (ChildDir.ToString() == cmbDrivesPrimaryDirectories.SelectedItem.ToString())
{
//recurse through the child directories.
//while (ChildDir.GetDirectories().Length > 0)
//{
DirectoryInfo[] grandChilds = ChildDir.GetDirectories();
foreach (DirectoryInfo grandChild in grandChilds)
{
FileInfo[] Files = grandChild.GetFiles(txtSearchType);
if (Files.Length == 0)
{
}
else
{
foreach (FileInfo DirFile in Files)
{
listBox1.Items.Add(DirFile.Name);
i++;
}
}
//ChildDir = GrandChild;
}
//}
}
}
}
catch (IOException E)
{
strMessage = E.Message;
listBox1.Items.Add(strMessage);
strMessage = "";
}
if (listBox1.Items.Count == 0)
{
listBox1.Items.Add(strMessage);
}
}
}
#endregion
#region protected void button2_Click
protected void button2_Click(object sender, System.EventArgs e)
{
//Close the list box.
this.Close();
}
#endregion
#region private void cmbDrives_SelectedIndexChanged
private void cmbDrives_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo[] childDirs = this.GetDirectories(cmbDrives.Text);
foreach (DirectoryInfo childDir in childDirs)
{
cmbDrivesPrimaryDirectories.Items.Add(childDir.ToString());
}
}
#endregion
}
}
It allows the user to select a drive and then one folder.
What would be the best to edit it so that it allows the user to search through a specific directory path?
namespace FindFiles
{
public partial class Form1 : Form
{
#region private void AddComboBoxItem
private void AddComboBoxItem(String[] strDrives)
{
for (int i = 0; i < strDrives.Length; i++)
{
cmbDrives.Items.Add(strDrives[i]);
}
}
#endregion
#region private void AddListBoxItem
private void AddListBoxItem()
{
listBox1.Items.Add(cmbDrives.Text);
}
#endregion
#region private String[] GetDrives
private String[] GetDrives()
{
return Directory.GetLogicalDrives();
}
#endregion
#region private Directory[] GetDirectories
private DirectoryInfo[] GetDirectories(String strDrive)
{
DirectoryInfo dir = new DirectoryInfo(strDrive);
DirectoryInfo[] childDirs = dir.GetDirectories();
return childDirs;
}
#endregion
#region public Form1
public Form1()
{
InitializeComponent();
String[] strDrives = GetDrives();
AddComboBoxItem(strDrives);
// Display the form as a modal dialog box.
ShowDialog();
}
#endregion
#region protected void button1_Click
protected void button1_Click(object sender, System.EventArgs e)
{
String strMessage = "No Matches";
listBox1.Items.Clear();
if (cmbDrives.Text == "")
{
lblHeader.Text = "Select the drive and Enter the file type to be searched!";
}
else if (cmbDrives.Text == "")
{
lblHeader.Text = "Select the drive to search the files!";
}
else
{
try
{
int i = 0;
//int iVal;
DirectoryInfo[] ChildDirs = this.GetDirectories(cmbDrives.Text);
String txtSearchType = "*.mp3";
//If the Search file type has no extention then add one.
//Get the Child Directories.
foreach (DirectoryInfo ChildDir in ChildDirs)
{
if (ChildDir.ToString() == cmbDrivesPrimaryDirectories.SelectedItem.ToString())
{
//recurse through the child directories.
//while (ChildDir.GetDirectories().Length > 0)
//{
DirectoryInfo[] grandChilds = ChildDir.GetDirectories();
foreach (DirectoryInfo grandChild in grandChilds)
{
FileInfo[] Files = grandChild.GetFiles(txtSearchType);
if (Files.Length == 0)
{
}
else
{
foreach (FileInfo DirFile in Files)
{
listBox1.Items.Add(DirFile.Name);
i++;
}
}
//ChildDir = GrandChild;
}
//}
}
}
}
catch (IOException E)
{
strMessage = E.Message;
listBox1.Items.Add(strMessage);
strMessage = "";
}
if (listBox1.Items.Count == 0)
{
listBox1.Items.Add(strMessage);
}
}
}
#endregion
#region protected void button2_Click
protected void button2_Click(object sender, System.EventArgs e)
{
//Close the list box.
this.Close();
}
#endregion
#region private void cmbDrives_SelectedIndexChanged
private void cmbDrives_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo[] childDirs = this.GetDirectories(cmbDrives.Text);
foreach (DirectoryInfo childDir in childDirs)
{
cmbDrivesPrimaryDirectories.Items.Add(childDir.ToString());
}
}
#endregion
}
}