0

I would like my code to skip or dismiss folders that give errors for whatever reasons. Currently, this code scans properly through folders but stops if any folder gives an error at it. Mostly hidden folders.

I have found these nearly identical questions but these answers only point to different directions not implementing the solution I need and I seem unable to implement any of the solutions into my code. Ive been banging my head for 3 days.

ignore "Unauthorized Access" " function Directory.GetDirectories()"

How to exclude folders when using Directory.GetDirectories

Ignore folders/files when Directory.GetFiles() is denied access

Hidden folders C#

My code is:

//encrypts target directory
    public void encryptDirectory(string location, string password)
    {

        //extensions to be encrypt
        var validExtensions = new[]
        {
            ".txt", ".doc"
        };

        string[] files = Directory.GetFiles(location);
        string[] childDirectories = Directory.GetDirectories(location);

        for (int i = 0; i < files.Length; i++){
            string extension = Path.GetExtension(files[i]);
            if (validExtensions.Contains(extension))
            {
                EncryptFile(files[i],password);
            }
        }
        for (int i = 0; i < childDirectories.Length; i++){
            encryptDirectory(childDirectories[i],password);
        }


    }

The closest answer I think might work is in the question: ignore "Unauthorized Access" " function Directory.GetDirectories()"

gives this code applied to files:

private static string[] GetFilesSafe(string location) {
try {
    return Directory.GetFiles(location);
} catch (UnauthorizedAccessException ex) {
    Console.Error.WriteLine(ex.Message);
    return new string[0];
}

}

Could someone please help me understand what I need in order to implement this type of solution or another within my code, applied to the folders? I know this is not the most efficient way to learn, but is time sensitive. Thanks for your/any help.

Tom
  • 19
  • 1
  • What exactly is not clear to you in the `GetFilesSafe` method implementation you provided? – dymanoid Oct 10 '18 at 10:50
  • I dont know where to put what. Im trying different combinations in visual studio that Im learning how to use, and I see errors everywhere. Apologize for not knowing how to code. – Tom Oct 10 '18 at 11:19
  • If your interested in a third party solution, there's a library (available via nuget) called AlphaFS that let's you specify options to automatically skip inaccessible directories/files. I am not affiliated, just a happy user – pinkfloydx33 Oct 10 '18 at 11:54
  • thanks, ill check it out. In any case im sure this must be an extremely simple problem, Im ok with ruby on rails and this is a non trivial problem. – Tom Oct 10 '18 at 12:02

1 Answers1

0

Since you want to Ignore or skip files that throws "Unauthorized Access"

 try {
    return Directory.GetFiles(location);
       //But you should alse check if the file is unavailable because it is:
        //being processed by another thread
        //or does not exist 
        // Not just for UnauthorizedAccessException !! 
} catch (UnauthorizedAccessException ex) {

        return true;
    }

You dont want to thow an error? You just want to continue. Also have you tried to run your aplication "as administrator", in that way you will be able to get aroung that UnauthorizedAccessException (if files are on same macihne where)

1392023093user
  • 1,047
  • 4
  • 21
  • 37
  • Runs on remote computer and its the unauthorized access to "the hidden folders" that is throwing the errors. At some point I managed to get it through but it stopped on the first file on the first folder. Then I changed, cant remember what I did. Where would I put my code within your example? thanks! – Tom Oct 10 '18 at 11:52