Answer the question
In order to leave comments, you need to log in
How to convert a file to UTF8 using C#?
Hello. There is a file:
c:\test.txt Encoded
win1251.
How to convert it to UTF8 (without BOM).
Or look for a library, or something :(
There are reflections on this topic on the stack, but they are more specialized (tasks).
That seems to be how it should work, but no :(
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
using(StreamReader reader = new StreamReader(@"d:\test.txt", Encoding.GetEncoding(1251)))
using(StreamWriter writer = new StreamWriter(@"d:\test.txt", false, Encoding.UTF8))
{
writer.Write(reader.ReadToEnd());
}
}
}
Answer the question
In order to leave comments, you need to log in
and so? (I'm not against streams, the general principle is the same .. just a lot of fuss already under the hood ;)
using System.IO;
using System.Text;
namespace ansi2utf8
{
class Program
{
static void Main(string[] args)
{
var t = File.ReadAllText("quest.txt", Encoding.Default);
File.WriteAllText("quest8.txt", t, Encoding.UTF8);
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question