I have the following code, it finds and displays empty folders, unfortunately it can't handle all folders, The Recycle bin and the App data folder cause access exceptions.
Further down is an example from another user that uses enumeration, with it I can access restricted folders but it can't handle long paths.
I'm trying the Delimon.Win32.IO; namespace from http://gallery.technet.microsoft.com/scriptcenter/DelimonWin32IO-Library-V40-7ff6b16c It can apparently handle long paths (I've not tested it yet)
I need a solution that can handle access restrictions and long paths - if possible.
private void button1_Click(object sender, EventArgs e)
{
//Open folder browser for user to select the folder to scan
DialogResult result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
{
//Clear text fields
listBoxResults.Items.Clear();
listBoxPath.Items.Clear();
txtFoldersFound.Clear();
//Store selected folder path
string dirPath = folderBrowserDialog1.SelectedPath;
//Process the folder
try
{
foreach (string dir in Directory.GetDirectories(dirPath, "*.*", SearchOption.AllDirectories))
{
//Populate List Box with all folders found
this.Invoke(new Action(() => listUpdate2(dir)));
if (Directory.GetDirectories(dir).Length.Equals(0))
{
//Populate List Box with all empty folders found
this.Invoke(new Action(() => listUpdate1(dir + Environment.NewLine)));
}
}
//Count of the empty folders
txtFoldersFound.Text = listBoxResults.Items.Count.ToString();
}
//Catch exceptions, seems to be folders not accessible causing this. Recycle Bin, App Data etc
catch (Exception err)
{
MessageBox.Show(err.Message);
}
}
}