0

actual question:

What is the best practice to modify text-files without changing their encoding?

Background info:

I am a bit ashamed that I fail to do this. What I actually want to do is what any texteditor does: Open, modify and save a large number of text-based files, without accidently modifying more than I want to.

string s = System.IO.File.ReadAllText(path);
System.IO.File.WriteAllText(path, s);

This will both use a default encoding. I can also provide a specific encoding as parameter. Both is not what I want. How do I easily autodetect the encoding to be able to read and write without changing it?

Best wishes, Janis

Janis
  • 436
  • 4
  • 12
  • 1
    _"How do I easily autodetect the encoding"_ - there is no such thing. It is not trivial, and all approaches are best-effort. If your modification only inserts or replaces ASCII text (character codes <= 127), you could treat any non-Unicode text file as binary (as Unicode detection is _somewhat_ easier) - barring character sets that don't even deal in ASCII. – CodeCaster Jun 02 '16 at 14:27
  • Then how do texteditor-tools manage to work properly? – Janis Jun 02 '16 at 15:32
  • 1
    I'm not saying it's impossible, I'm saying it's not trivial. There can always be some ambiguity. – CodeCaster Jun 02 '16 at 15:33
  • Thanks. This is supposed to be a rather common problem: How come the System.IO-Library does not provide the means for a simple "best-effort solution" and instead uses only the default encodings? – Janis Jun 02 '16 at 15:34
  • 1
    I guess because it's not deterministic. I wouldn't want to support such a feature, and I guess MS didn't either. – CodeCaster Jun 02 '16 at 15:42

0 Answers0