I am trying to get a listing of all files on a given drive into a listbox, however when I set the starting point as the root of the drive (eg: G:) it will not do anything, but if I start in a folder on the drive, it works as expected (eg: G:\Downloads). My code is below:
private void btnSelectDirectory_Click(object sender, EventArgs e)
{
lstFileNames.Items.Clear();
if(fbd.ShowDialog() == DialogResult.OK)
{
strSelectedFolder = fbd.SelectedPath;
}
try
{
string[] files = System.IO.Directory.GetFiles(strSelectedFolder, "*.*",SearchOption.AllDirectories);
foreach (string s in files)
{
System.IO.FileInfo fi = null;
try
{
fi = new System.IO.FileInfo(s);
}
catch (System.IO.FileNotFoundException ex)
{
MessageBox.Show(ex.Message);
continue;
}
finally
{
lstFileNames.Items.Add(fi.Directory + @"\" + fi.Name);
}
}
}
catch (Exception ex)
{
}
}