Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnauthorizedAccessException while scanning directory 'User\Documents\My Music'

  1. Question: Why do I get this error while scanning a users 'My Documents' folder, but not when I scan the 'My Music/My Pictures/My Videos' directory?
  2. Secondary, less important question: Is there a way to avoid this without having to specifically filter these folders out, or using a try/catch block?

I prefer answers that teach me how to fish, instead of just giving me fish. Just at this point I am not sure where I need to look to specifically answer this question. I've read through documents about elevating permissions and iterating through the file system, and spent a good week looking for why I can set DirectoryInfo on 'User\My Music' but not 'User\Documents\My Music'(link) and just would enjoy a little boost in a different direction in regards to learning more.

I catch the initial 'UnauthorizedAccessException' that is thrown initially when attempting Directory.GetFiles('path', "*", SearchOption.AllDirectories) where path is the users 'My Documents'. To handle the exception I know that I need to walk the directory manually. Which works, returning the files from the sub-directories.

The code for the initial GetFiles function:

public static string[] GetFiles(string path)
{
    string[] files;
    try
    {
        files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
    }
    catch(UnauthorizedAccessException ex)
    { files = WalkDirectory(path); }
    return files;
}

public static string[] WalkDirectory(string path)
{
    List<string> files = new List<string>();
    DirectoryInfo dir = new DirectoryInfo(path);

    foreach (DirectoryInfo subDir in dir.GetDirectories())
    {
        try
        {
            files.AddRange(WalkDirectory(subDir.FullName));
        }
        catch(UnauthorizedAccessException ex)
        {
            // complete fail to walk directory listed
            throw ex;
        }
    }
    foreach (FileInfo file in dir.GetFiles())
    {
        files.Add(file.FullName);
    }
}

This works out perfectly, until the code attempts to walk the hidden folders: My Music, My Pictures, or My Videos. No matter how I try and re-code to walk the hidden files, I keep receiving the UnauthorizedAccessException.

I understand completely that I am going to code around this. Mainly what I am curious to know, is why is the exception happening under a users folder?

An asssumption I am making is that the folder is a symlink to another directory, because I can make the path ?:\users directory\user\My (Music, Pictures, or Videos) and the code walks those directories then without any issues. This only happens when trying to scan the directory files after setting them from within the users My Documents.

  • OS: Windows 7
  • User Privliages: Administrator
  • Application Elevated to run as administrator
like image 267
Riana Avatar asked May 07 '12 05:05

Riana


1 Answers

I was speaking about this with a friend, who is not technical, but knows enough tech to hold a conversation and he helped me narrow this question down further. This is actually a duplicate question and was answered at Check if a file is real or a symbolic link.

The folder is a symbolic link that was placed there for backwards compatibility purposes according to this article on TechRepublic: Answers to some common questions about symbolic links under the section Windows Vista and Windows 7 have built-in symbolic links paragraph 2.

In order to specifically avoid attempting to scan this directory without a Try/Catch block on an UnauthorizedAccessException the folder attributes need to be checked to determine if the folder or file in question is a symbolic link. Which again was answered in the above listed stackoverflow question.

like image 166
Riana Avatar answered Nov 15 '22 11:11

Riana