- 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) {
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
source share