DirectoryNotFoundException when calling Directory.GetDirectories on Environment.SpecialFolder.Favorites due to domain folder redirection

I have a C # code that is trying to get Favorites for the current user. The code is part of the taskbar toolbar, which is loaded into the Windows Explorer process. I have a user who uses Windows Vista with UAC enabled in a domain that is configured or enabled in the Roaming Profile or Folder Redirection features. When Directory.GetDirectories is called on the Favorites path, it throws "System.IO.DirectoryNotFoundException: Could not find part of the path" C: \ Users \\ Favorites \ ". Other users in other domains that do not have roaming profiles or folder redirection, don't have this problem.

The user also said that copying the path from failed logs to the launch prompt cannot load the path, but if they navigate the path directly with Explorer, then copy and paste this path into the launch prompt, it works. He sent me both ways, and they are absolutely identical, which makes no sense.

My theory is that this is caused by folder redirection, where this path actually points to a share on the server, but the redirection does not work when trying to access subdirectories (help information returned from Directory.GetDirectories). The source directory works, but all subdirectories of the source directory cannot be redirected correctly.

Does anyone encounter a situation like this and / or knows a workaround to get correct access to the redirected folders?

private void GetFavorites() { try { System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites)); AddFavorites(dirInfo); } catch { } } private void AddFavorites(DirectoryInfo dirInfo) { foreach (System.IO.FileInfo fileInfo in dirInfo.GetFiles("*.url")) { //string alias = fileInfo.Name.Replace(".url", ""); if (!ItemsBookmarks.ContainsKey(fileInfo.Name)) ItemsBookmarks.Add(fileInfo.Name, fileInfo.Name); } foreach (System.IO.FileInfo fileInfo in dirInfo.GetFiles("*.lnk")) { if (!ItemsBookmarks.ContainsKey(fileInfo.Name)) ItemsBookmarks.Add(fileInfo.Name, fileInfo.Name); } foreach (System.IO.DirectoryInfo objDir in dirInfo.GetDirectories()) { AddFavorites(objDir); } } 

Thanks,

John

+4
source share
1 answer

I believe that the problem you are facing is with the re-check points.

See: http://msdn.microsoft.com/en-us/library/bb513869.aspx

See: What is the best way to check for a reprocessing point in .net (C #)?

The problem can be avoided by using the following syntax:

 private void AddFavorites(string dirPath) { try { foreach (string fileName in Directory.GetFiles(dirPath, "*.*", SearchOption.TopDirectoryOnly)) { //string alias = fileInfo.Name.Replace(".url", ""); if (!ItemsBookmarks.ContainsKey(fileInfo.Name)) { ItemsBookmarks.Add(fileName); } } foreach (string subDirName in Directory.GetDirectories(dirPath, "*.*", SearchOption.TopDirectoryOnly)) { AddFavorites(objDir); } } catch { //error getting files or subdirs... permissions issue? //throw } } 
+1
source

All Articles