Search code examples
c#localefileinfo

Problems with FileInfo


I'm an italian user and I've these directories:

  • Utenti (as Users)
  • Immagini (as Pictures)
  • Etc..

Now, when I search a file with Windows SDK DSearch API, I get this path:

C:\Utenti\\myfile.txt

and when I invoke:

FileInfo file = new FileInfo(@"C:\Utenti\<User>\myfile.txt");

the field file.Length throws an exception (FileNotFound). But if I use:

FileInfo file = new FileInfo(@"C:\Users\<User>\myfile.txt");

file.Length works correctly.

How I can solve this issue?


Solution

  • Use Environment.GetFolderPath(Environment.SpecialFolder....)); to retrieve the folder you need.

    If you get from somewhere else a localized path, you can try to match with the actual special folder localized to by looking at this question: How to get the actual (localized) folder names? Basically this tour out in using ( interop ) the SHGetLocalizedName function.