UnauthorizedAccessException when scanning the "User \ Documents \ My Music" directory

  • Question:. Why am I getting this error when scanning the My Documents folder of users, but not when scanning the My Music / My Photos / My Videos directory?
  • A secondary, less important issue . Is there a way to avoid this without trying to specifically filter these folders or use a try / catch block?

I prefer answers that teach me how to fish, rather than just give me fish. Just at this moment I am not sure where I need to specifically answer this question. I read the docs on increasing permissions and iterating through the file system , and spent a good week searching why I can set DirectoryInfo to "User \ My Music" but not "User \ Documents \ My Music" (link), and it just will be a little push you in a different direction with regard to more details.

I will catch the original "UnauthorizedAccessException", which is initially raised when trying Directory.GetFiles ("path", "*", SearchOption.AllDirectories), where the path is "My Documents" of users. To deal with this exception, I know that I need to manually manage the directory. What works by returning files from subdirectories.

Code for the GetFiles initial 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 fine until the code tries to find hidden folders: My Music, My Pictures, or My Videos. No matter how I try to transcode hidden files, I keep getting UnauthorizedAccessException.

I fully understand that I'm going to code this. Basically, what Iā€™m interested to know is why the exception occurs in the users folder?

It seems to me that the folder is a symbolic link to another directory, because I can make the path ?: \ Users directory \ user \ My (Music, Pictures or Videos) and the code goes through these directories without any problems. This only happens when you try to scan directory files after they are installed inside My Documents users.

  • OS: Windows 7
  • User Privliages: Administrator
  • Application upgraded to run as administrator
+4
source share
1 answer

I spoke about this with a friend who is not technical, but knows enough technique to conduct a conversation, and he helped me narrow this question further. Actually, this is a duplicate question, to which the answer was received Check whether the file or the symbolic link is valid .

A folder is a symbolic link that was placed there for backward compatibility in accordance with this article in TechRepublic: Answers to some common questions about symbolic links in the Windows Vista and Windows 7 section have built-in symbolic links .

To avoid trying to scan this directory without a Try / Catch block in UnauthorizedAccessException, folder attributes should be checked to determine if the specified folder or file is a symbolic link. Which was again answered in the stackoverflow question above.

+2
source

Source: https://habr.com/ru/post/1411054/


All Articles