Search code examples
c#directorypath-separator

C# Bilingual Directory Separator Character for Non-English Windows


I am trying to create-read/write a file into a subfolder of the users AppData\Roaming folder:

string fileloc = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FolderName" + Path.AltDirectorySeparatorChar + "SomeFile.txt");

This is working brilliantly on my computer, but when I ran the program on a friend's Japanese laptop (which uses ¥ as its directory separator) they were only able to read/write to the file, and the program would crash if it needed to create the file. (I also tried the non-Alt directory separator.)

The string fileloc printed:

C:¥Users¥UserName¥Appdata¥Roaming¥FolderName/SomeFile.txt


Solution

  • How about

    string fileloc = Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FolderName"), "SomeFile.txt");
    

    Or, perhaps easier to comprehend:

    string directoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FolderName"); 
    string fileloc = Path.Combine(directoryPath, "SomeFile.txt");