Write Unicode String In a File Using StreamWriter doesn't Work
You never Close()
the StreamWriter
.
If you call writer.Close()
when you finish writing, you will see the character.
But, since it implements IDisposable
you should wrap the creation of the StreamWriter
in a using
statement:
using(StreamWriter writer = new StreamWriter("a.txt", false, Encoding.UTF8))
{
writer.WriteLine(s);
}
This will close the stream for you.
By the looks of it you're not Flush()
ing or Close()
ing the StreamWriter
before you end your application. StreamWriter
uses a buffer internally which needs to be flushed before you close your application or the StreamWriter
goes out of scope otherwise the data you wrote to it will not be written to disc.
You can call Close()
once you're done - although instead I would suggest using a using
statement instead to also assure that your StreamWriter
gets properly disposed.
string s = "آ";
using (StreamWriter writer = new StreamWriter("a.txt", false, Encoding.UTF8))
{
writer.WriteLine(s);
}
Try using File.WriteAllText("a.txt", s, Encoding.UTF8);
.