How to write a string to a file in non-default encoding mode when use AppendText() function.

W

Wootaek Choi

I've tried to record some data to the file with C#.
The data should be added in the end of log file,
And If there is no log file, A new log file should be created to write
the data

for that reason, AppendText() function used to open the log file.
but AppendText() returns StreamWriter object using utf-8 encode mode.

What shall i do to get a StreamWriter object using the other encode
mode when i use AppendText()?

Here is my code...

+--------------------------------------------------------------------
| FileInfo logFile = new FileInfo(dirInfo.FullName+"\\"+path);
| StreamWriter writer = logFile.AppendText();
| writer.WriteLine(
| "["+DateUtility.getTimeIn24Format() +"] "+
| logStr.Replace("\n",writer.NewLine) );
| writer.Close();
+---------------------------------------------------------------------
 
J

Jon Skeet [C# MVP]

Wootaek Choi said:
I've tried to record some data to the file with C#.
The data should be added in the end of log file,
And If there is no log file, A new log file should be created to write
the data

for that reason, AppendText() function used to open the log file.
but AppendText() returns StreamWriter object using utf-8 encode mode.

What shall i do to get a StreamWriter object using the other encode
mode when i use AppendText()?

Rather than using AppendText, create a new FileStream (with the
appropriate mode to append) and then create a StreamWriter from that,
specifying the encoding you wish to use.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top