D
D
dimadimov2016-02-17 21:54:30
Programming
dimadimov, 2016-02-17 21:54:30

What's going on with C# file encoding?

using System;
using System.IO;

namespace ConsoleApplicationF
{
    class Program
    {
        
        static void Main(string[] args)
        {           
            string F1 = @"C:\F\f1.txt";
            string F2 = @"C:\F\f2.txt";        

            string CopyF2= "", CopyF1="";

            StreamReader FF1 = new StreamReader(F1, System.Text.Encoding.Default);
            CopyF1= FF1.ReadToEnd();
            FF1.Close();

            StreamReader FF2 = new StreamReader(F2, System.Text.Encoding.Default);
            CopyF2=FF2.ReadToEnd();
            FF2.Close();

            StreamWriter FF11 = new StreamWriter(F1);
            FF11.Write(CopyF2);
            FF11.Close();

            StreamWriter FF22 = new StreamWriter(F2);
            FF22.Write(CopyF1);
            FF22.Close();

  }
}
}

I'm trying to replace text from one file with text from another.
You can do this, but I want to better understand all this!
using System;
using System.IO;
namespace ConsoleApplication14
{
    class Program
    {
        static void Main(string[] args)
        {           
            string F1 = @"C:\F\f1.txt";
            string F2 = @"C:\F\f2.txt";

            string F3 = @"C:\F\dewfdewdfhtfhgfchjgfvjhygjudfew.txt";
            File.Replace(F1, F2, F3);
            File.Move(F3, F1);
        }
}
}

I enter values ​​into files:
8f1127fe542e443395d60f84e0e0c210.jpg
I run the program:
699f3762982c4f71b82d37595a6cc174.jpg
I run the program a second time:
5f99f8aba47e444499a7f60c23c4b54d.jpg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Makarov, 2016-02-17
@dimadimov

Judging by the screenshot, you have lost the BOM for UTF-8.
Most likely this happened because you considered the file to be the wrong encoding (in the file you seem to have UTF-8). Use an explicit encoding, such as UTF8Encoding instead of the default one.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question