Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write Unicode String In a File Using StreamWriter doesn't Work

I have this code:

string s = "آ";
StreamWriter writer = new StreamWriter("a.txt", false, Encoding.UTF8);
writer.WriteLine(s);

but when I run it I can't see any "آ" in a.txt!! There isn't any string in a.txt! It is Empty! What is problem!?! Can anyone help me???

like image 239
ahmadali shafiee Avatar asked Nov 19 '11 19:11

ahmadali shafiee


People also ask

Where does StreamWriter write to?

StreamWriter. WriteLine() method writes a string to the next line to the steam. The following code snippet creates and writes different author names to the stream.

Will StreamWriter create a file?

The first step to creating a new text file is the instantiation of a StreamWriter object. The most basic constructor for StreamWriter accepts a single parameter containing the path of the file to work with. If the file does not exist, it will be created. If it does exist, the old file will be overwritten.

Which is StreamWriter class's method?

C# StreamWriter class is used to write characters to a stream in specific encoding. It inherits TextWriter class. It provides overloaded write() and writeln() methods to write data into file.


1 Answers

Try using File.WriteAllText("a.txt", s, Encoding.UTF8);.

like image 105
MagnatLU Avatar answered Oct 20 '22 20:10

MagnatLU