This project is read-only.

How do you exactly skip Unauthorized Access Exception files/folders with AlphaFS

Oct 7, 2014 at 1:49 PM
I found this https://alphafs.codeplex.com/discussions/478008 but don't know exactly how to implement it.

This is the line I want it on
foreach (string dir in Directory.EnumerateDirectories(dirPath, "*.*", SearchOption.AllDirectories))
Also if it skips files and folders it can't access will it be picked up in a Try/Catch block?
try
                {
                    //foreach (string dir in Directory.GetDirectories(dirPath, "*.*", SearchOption.AllDirectories))
                    foreach (string dir in Directory.EnumerateDirectories(dirPath, "*.*", SearchOption.AllDirectories))
                    {
                        //Populate List Box with all folders found
                        this.Invoke(new Action(() => listUpdate2(dir + Environment.NewLine)));
                        //Check and report on all empty folders
                        if (Directory.GetFiles(dir).Length == 0 && Directory.GetDirectories(dir).Length == 0)
                        {
                            this.Invoke(new Action(() => listUpdate1(dir + Environment.NewLine)));
                            //Directory.Delete(dir, false);
                        }
                    }
                 }
                //Catch exceptions
                catch (UnauthorizedAccessException err)
                {
                    // This code should just write out the message and continue to recurse. 
                    log.Add(err.Message);
                }
                finally
                {
                    //add a count of the empty folders here
                    txtFoldersFound.Text = listBoxResults.Items.Count.ToString();
                    MessageBox.Show("Scanning Complete", "Done", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    // Write out all the files that could not be processed.
                    foreach (string s in log)
                    {
                        this.Invoke(new Action(() => listUpdate1(s)));
                    }
                    log.Clear();
                }
Developer
Oct 7, 2014 at 3:13 PM
Edited Oct 7, 2014 at 3:13 PM
You need to use the overloaded method from the AlphaFS namespace: Alphaleonis.Win32.Filesystem.Directory

Alphaleonis.Win32.Filesystem.Directory.EnumerateDirectories(string path, string searchPattern, SearchOption searchOption, bool continueOnException);


Any Exceptions encountered during enumeration are lost.

Yomodo
Oct 8, 2014 at 1:03 AM
Hi Yomodo

Excuse my ignorance, I'm new to C#.

I still can't get your example to work, I don't understand how to implement (bool continueOnException)
foreach (string dir in Alphaleonis.Win32.Filesystem.Directory.EnumerateDirectories(dirPath, "*.*", SearchOption.AllDirectories, continueOnException(true)))
Cheers
Developer
Oct 8, 2014 at 7:24 AM
You almost had it:
foreach (string dir in Alphaleonis.Win32.Filesystem.Directory.EnumerateDirectories(dirPath, "*.*", SearchOption.AllDirectories, true))
Marked as answer by Yomodo on 10/10/2014 at 12:19 AM
Oct 9, 2014 at 2:17 AM
Hi Yomodo

DOH! that was easy...

However AlphaFS is stopping on folders it cannot access instead of skipping that folder, I thought this is what it is meant to do? For example I browsed to the My Documents folder (In Windows 7) and it stoppped on My Music, it didn't get to My Pictures etc.

This is the code, Am I doing something wrong with how AplhaFS is meant to be applied?
using Alphaleonis.Win32.Filesystem;
using SearchOption = System.IO.SearchOption;
private void button1_Click(object sender, EventArgs e)
        {
            //Clear text fields
            listBoxResults.Items.Clear();
            listBoxPath.Items.Clear();
            txtItemsFound.Text = String.Empty;

            //Open folder browser for user to select the folder to scan
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {   
                //Store selected folder path
                string dirPath = folderBrowserDialog1.SelectedPath;
                
                //Process the folder
                ThreadStart t = delegate
                {
                try
                {
                    //foreach (string dir in Directory.EnumerateDirectories(dirPath, "*.*", SearchOption.AllDirectories))
                    //foreach (string dir in Directory.GetDirectories(dirPath, "*.*", SearchOption.AllDirectories))
                    foreach (string dir in Alphaleonis.Win32.Filesystem.Directory.EnumerateDirectories(dirPath, "*.*", SearchOption.AllDirectories, true))
                    {
                        //Populate List Box with all folders found
                        this.Invoke(new Action(() => listUpdate2(dir + Environment.NewLine)));
                        //Check and report on all empty folders
                        if (Directory.GetFiles(dir).Length == 0 && Directory.GetDirectories(dir).Length == 0)
                        {
                            this.Invoke(new Action(() => listUpdate1(dir + Environment.NewLine)));
                            //Directory.Delete(dir, false);
                        }
                    }
                 }
                //Catch exceptions
                catch (Exception err)
                {
                    // This code should just write out the message and continue to recurse. 
                    if (IsCritical(err))
                    log.Add(err.Message);
                }
                finally
                {
                    //Add a count of the empty folders here
                    txtItemsFound.Text = listBoxResults.Items.Count.ToString();
                    // Write out all the files that could not be processed.
                    foreach (string s in log)
                    {
                        this.Invoke(new Action(() => listUpdate1(s)));
                    }
                    log.Clear();
                    MessageBox.Show("Scanning Complete", "Done", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                }; new Thread(t).Start();
            }
        }
Developer
Oct 9, 2014 at 7:36 AM
Hi RockE,

What exactly do you mean with "it stops" ?
Your app hangs? Or exits with an Exception?

It could also "stop" (showing no activity) because browsing/enumerating the My Documents folders is kind of dangerous.
Well dangerous in that you could end up with a loop since My documents also consists of Reparse Points..

Try browsing something "simpler" first, like C:\Windows folder for example.
Oct 9, 2014 at 7:49 AM
Hi Yomodo

Attempting to browse the Windows folder works until it hits a folder called ccmcache it then stops recursing and goes to the finally section and writes to my listbox

Access is denied C:\Windows\ccmcache

Perhaps because I'm recursing using Enumeration first but then actually using Directory.Getdirectories to test for empty folders?

if (Directory.GetFiles(dir).Length == 0 && Directory.GetDirectories(dir).Length == 0)

Cheers
Developer
Oct 9, 2014 at 7:53 AM
Yes, you should also put the Getfiles()/Getdirectories() inside a try catch.
Oct 10, 2014 at 5:49 AM
Edited Oct 10, 2014 at 5:49 AM
Hi Yomodo

Getfiles/GetDirectories is inside the try/catch/finally.

Should I be using a Alphaleonis.Win32.Filesystem.Directory.Getfiles/GetDirectories (If there is such an option?)

Cheers
Developer
Oct 10, 2014 at 8:18 AM
AlphaFS has full implementations of the File and Directory classes.

Each method that can throw an exception should be wrapped in its own try/catch.

Try not to mix System.IO and AlphaFS, If there is a reason to use AlphaFS, stick with that.
Oct 18, 2014 at 12:31 PM
Hi Yomodo

Thanks for your help, adding the 2nd try/catch block fixed the issues I was having.

Cheers

RockE