L
L
LebedevStr2019-03-03 23:44:36
Character encoding
LebedevStr, 2019-03-03 23:44:36

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());
         }
      }
   }

There are no errors.
Libraries are connected, but nothing happens.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
#
#, 2019-03-04
@LebedevStr

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);
        }
    }
}
spoiler
Jf3pH9u.pngm36z6IT.png
вся папка проекта с компиляцией и тд..
https://www.sendspace.com/file/a635b5

J
John_Nash, 2019-03-04
@John_Nash

must have different filenames

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question