Recursion exception through the SpecialFolder directory on a non-English OS

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

However, I have the following problems on non-English installations:

  • When I use non-localized paths, I get an UnauthorizedAccessException for any subfolders I'm trying to access

  • If I localize the result of Environment.GetFolderPath and try to get a list of subdirectories, I get a DirectoryNotFoundException on the localized path. An example of a 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 look at the addresses 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 overwrite the directory path returned by the .GetFolderPath environment when a start point is specified at which the start directory may contain reprocessing and / or connection points.

+8
c # windows filesystems winapi
source share
1 answer

AFAIK, a localized system folder is just aliases, if you run cmd and dir on your main drive, you will see that the "users" folder is called, well, "Users", regardless of your system language, so check your paths. I have confirmed this since 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 don’t have enough data to diagnose your problem, but you can check your function by getting random paths from the FolderBrowserDialog and passing them to debug your function, see which one works and which doesn't, look, the main call that fails or is one of the recursions ...

+1
source share

All Articles