Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# System.IO.Directory.CreateDirectory - create directory with utf-8 characters?

Tags:

c#

utf-8

I read data from utf-8 encoded file. Part of content of this file is then used as a name for newly created folder. However, my folder name is:

bohou�_120328 instead of bohouš_120328

How can I set proper coding for the name of newly created folder? Thanks.

edit:

I am reading information from file this way:

System.IO.StreamReader file = new System.IO.StreamReader(nameOfFile);

 while ((j = file.ReadLine()) != null) { 
    //manipulating string in j
 }

then creating directory with

if (Directory.Exists(folder) == false) {
                                    Console.WriteLine("creating directory " + folder);
                                    System.IO.Directory.CreateDirectory(@folder);
                                } 

If I run my application on my Windows 7, 64bit computer, everything is fine. However, if I run in on other computers with older systems like WinXP, coding is just wrong and looks like this

bohou�_120328

Before using variable to creating folder, I write i to output, but everything is fine. Even folder names are fine. But just on my computer, unfortunately.

edit2:

Things are getting even more weird. I used this code How do I remove diacritics (accents) from a string in .NET? to remove diacritics, because names without diacritics are just fine for me.

However, again:

  1. running code on my computer yields into bohous_120328
  2. running code on other computers AND my flash disk yields into bohou�_120328

I swear it is the same code, as I COPIED my .exe file.

Debugger says that the problem is already in my string variable before creating folder. I do not understand, how the environment influences my variables in this case.

Will be happy for explanation :-)

like image 939
Perlnika Avatar asked Dec 12 '22 02:12

Perlnika


1 Answers

On Windows, you do not specify encoding of file or directory names. On NTFS they are always encoded with what is essentially UTF-16. As long as you read in the string correctly, CreateDirectory will do what you want. I'd suspect that you either didn't read your UTF-8 file as UTF-8, or your file isn't actually UTF-8. Take a look in the debugger what the string value is before you call CreateDirectory with it.

like image 187
Jason Malinowski Avatar answered May 10 '23 01:05

Jason Malinowski