Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception recursing through SpecialFolder directory on non-english OS

I am trying to recurse through a SpecialFolder (Environment.SpecialFolder.StartMenu and Environment.SpecialFolder.DesktopDirectory) in my application and it works for English installations.

However I am having the following issues on non-English installations:

  1. When I use the non localized paths I get UnauthorizedAccessException for any subfolders I try to access

  2. If I localize the result of Environment.GetFolderPath and attempt to get a listing of sub directories I get a DirectoryNotFoundException on the localized path. An example of the localized path:

Original -> C:\Users\tony\AppData\Roaming\Microsoft\Windows\Start Menu

Localized -> C:\Utilisateurs\tony\AppData\Roaming\Microsoft\Windows\Menu Démarrer

I use Environment.GetFolderPath to get the directory, and then search through the locations for a specific file with the following method:

private static IEnumerable<string> LocateAppShortcut(string dir)
{
    foreach (string directory in Directory.GetDirectories(dir))
    {
        foreach (string file in Directory.GetFiles(directory, "MyApp.appref-ms"))
        {
            yield return file;
        }

        foreach (string file in LocateAppShortcut(directory))
        {
            yield return file;
        }
    }
}

I am looking for a method that will allow me to reliably recurse through a directory path returned by Environment.GetFolderPath when given a starting point where the starting directory can contain reparse and/or junction points.

like image 299
dmck Avatar asked Mar 28 '14 19:03

dmck


1 Answers

AFAIK, localized system folder are just aliases, if you run a cmd and dir your main drive, you'll see that the "users" folder is called, well, "Users", independently of your system language, so check your paths. I've confirmed this, as my system is configured in Spanish:

C:\>dir
 El volumen de la unidad C es ----------
 El número de serie del volumen es: ---------

 Directorio de C:\

10/12/2013  12:26    <DIR>          inetpub
06/10/2013  17:51    <DIR>          Intel
18/02/2014  14:34    <DIR>          Mis lugares Web
03/12/2013  17:52    <DIR>          NVIDIA
22/08/2013  17:22    <DIR>          PerfLogs
24/02/2014  14:35    <DIR>          Program Files
12/06/2014  09:18    <DIR>          Program Files (x86)
18/09/2013  20:41    <DIR>          Toshiba
10/12/2013  12:42    <DIR>          Users
11/04/2014  15:08    <DIR>          Windows
               0 archivos              0 bytes
              11 dirs  664.620.318.720 bytes libres

C:\>

I dno't have enough data to diagnose your problem, but you could test your function by getting random paths from a FolderBrowserDialog and passing them to debug your function, see which of them work and which doesn't, see if it's the main call which is failing or is it one of the recursions...

like image 66
Caótico fanegas Avatar answered Oct 26 '22 22:10

Caótico fanegas