Forcing StreamWriter to change Encoding
Solution 1
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.
Solution 2
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();
}
}
Solution 3
There is a StreamWriter(string path, bool append, Encoding encoding) constructor - you could just explicitly specify the append flag too?
I said you ought to wrap your StreamWriter in a using
too, i.e.
if (dr == DialogResult.OK)
{
using(StreamWriter sw = new StreamWriter(saveFileDialog1.FileName, false, Encoding.UTF8)) {
sw.Write(sb.ToString());
sw.Close();
}
}
although realistically this won't make any difference here. This effectively puts a try/finally around the code so that the StreamWriter will get cleaned up (it'll call sw.Dispose()
even if an exception gets thrown in the meantime. (Some people will say this also means you no longer need the .Close
since the Dispose will take care of that too but I prefer to have it anyway.)
Solution 4
setting UTF8 encoding working with Arabic font is the best thing I did:
using (var sw = new StreamWriter(
new FileStream(temporaryFilePath,
FileMode.Create,
FileAccess.ReadWrite),
Encoding.UTF8))
{
sw.Write(sb.ToString());
}
)
Solution 5
The easiest way is to use the right constructor.
StreamWriter(String, Boolean, Encoding)
Initializes a new instance of the StreamWriter class for the specified file by using the specified encoding and default buffer size. If the file exists, it can be either overwritten or appended to. If the file does not exist, this constructor creates a new file.
C#
public StreamWriter (string path, bool append, System.Text.Encoding encoding);
EMBEDONIX
Protecting yourself against thrown exceptions, considered as a defensive programming practice.
Updated on January 08, 2020Comments
-
EMBEDONIX over 4 years
I am trying to save a file using
DialogResult
andStringBuilder
. 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
asEncoding.UTF8
but since the first argument is astring
rather than aStream
, 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