0

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)
        {

        }
    }
Tornado726
  • 352
  • 1
  • 7
  • 16
  • _G:_ I believe it is supposed to be G:\ – Euphoric Sep 10 '16 at 19:49
  • 1
    I doesn't happen anything because you swallow the exception. Never write empty try/catch blocks, you will never know what is wrong in your code. – Steve Sep 10 '16 at 19:52
  • 1
    [Pokemon exception handling](http://www.dodgycoder.net/2011/11/yoda-conditions-pokemon-exception.html) is likely to blame for lack of information about the problem. There are plenty of folders at the root that will give you AccessDeniedException... (And plenty of questions how to deal with it too). – Alexei Levenkov Sep 10 '16 at 19:53
  • Duplicate (http://stackoverflow.com/questions/5098011/directory-enumeratefiles-unauthorizedaccessexception) have copy-paste ready code you need. – Alexei Levenkov Sep 10 '16 at 19:55

0 Answers0