Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing StreamWriter to change Encoding

I am trying to save a file using DialogResult and StringBuilder. After making the text, I am calling the following code to save the file:

    if (dr == DialogResult.OK)     {          StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);          sw.Write(sb.ToString());         sw.Close();     } 

I tried to add the second parameter to StreamWriter as Encoding.UTF8 but since the first argument is a string rather than a Stream, it does not compile it.

How can I convert that string to a stream to be able to pass the second parameter as Encoding?

The reason for this, is that somewhere in my text I have µ but when the file is saved it shows like μ so the µ is getting screwd!

Thanks

like image 966
Saeid Yazdani Avatar asked Nov 16 '11 12:11

Saeid Yazdani


People also ask

What is the default encoding for StreamWriter?

StreamWriter(Stream) Initializes a new instance of the StreamWriter class for the specified stream by using UTF-8 encoding and the default buffer size.

What is the difference between StreamWriter and TextWriter?

The StreamWriter class in C# is used for writing characters to a stream. It uses the TextWriter class as a base class and provides the overload methods for writing data into a file. The StreamWriter is mainly used for writing multiple characters of data into a file.

What does a StreamWriter do?

StreamWriter contains methods to write to a file synchronously (Write and WriteLine) or asynchronously (WriteAsync and WriteLineAsync). File provides static methods to write text to a file, such as WriteAllLines and WriteAllText, or to append text to a file, such as AppendAllLines, AppendAllText, and AppendText.

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.


2 Answers

Just wrap it in a FileStream.

StreamWriter sw = new StreamWriter(     new FileStream(saveFileDialog1.FileName, FileMode.Open, FileAccess.ReadWrite),     Encoding.UTF8 ); 

If you want to append, use FileMode.Append instead.

You should also call Dispose() on a try/finally block, or use a using block to dispose the object when it exceeds the using scope:

using(     var sw = new StreamWriter(         new FileStream(saveFileDialog1.FileName, FileMode.Open, FileAccess.ReadWrite),         Encoding.UTF8     ) ) {     sw.Write(sb.ToString()); } 

This will properly close and dispose the streams across all exception paths.

UPDATE:

As per JinThakur's comment below, there is a constructor overload for StreamWriter that lets you do this directly:

var sw = new StreamWriter(saveFileDialog1.FileName, false, Encoding.UTF8); 

The second parameter specifies whether the StreamWriter should append to the file if it exists, rather than truncating it.

like image 106
Polynomial Avatar answered Sep 20 '22 15:09

Polynomial


There is a constructor for filename, appendMode, encoding.

With a proper using block it looks like:

if (dr == DialogResult.OK) {     using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName,             false, Encoding.UTF8))     {       sw.Write(sb.ToString());       //sw.Close();     } } 
like image 28
Henk Holterman Avatar answered Sep 21 '22 15:09

Henk Holterman