3

File.WriteAllText is inserting a space after every letter and quotation.

Example:

Original File

"JobID" "ParentJobID"

New File

" J o b I D "    " P a r e n t J o b I D "

CODE

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProcessOutputLogTransfer
{
    class Program
    {
        static void Main(string[] args)
        {

            string content = File.ReadAllText(@"C:\Documents and Settings\All Users\Application Data\Microsoft\Windows NT\MSFax\ActivityLog\OutboxLOG.txt");

        File.WriteAllText(@"C:\FAXLOG\OutboxLOG.txt", content, Encoding.UTF8);
        }
    }
}
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
Jake H.
  • 563
  • 2
  • 11
  • 23
  • What encoding is the file in originally? – Oded Dec 21 '11 at 14:52
  • It's possible the original file was written under a different encoding. Try reading a different file (one you've created in notepad, for example) and try different encodings when writing back out. – Samuel Slade Dec 21 '11 at 14:52
  • What does content look like before you write it out? Are the spaces showing there? – shf301 Dec 21 '11 at 14:53
  • Obligatory: http://www.joelonsoftware.com/articles/Unicode.html – David Dec 21 '11 at 14:54
  • The original file is utf 16. That is the whole point of doing this so I can convert it and move it into another location. Otherwise I will not be able to import it into MySQL. I am using notepad to preview the file C# wrote. However, if I open the utf 16 in notepad and resave it as a utf 8 it comes out fine and I am able to import it into MySQL. – Jake H. Dec 21 '11 at 14:55

6 Answers6

8

I don't think it's WriteAllText that's doing this. I believe it's ReadAllText, which defaults to reading using UTF-8 - I suspect your OutboxLOG.txt file is actually written in UTF-16, instead. Try this:

string inputPath = @"C:\Documents and Settings\All Users\Application Data\"
                 + @"Microsoft\Windows NT\MSFax\ActivityLog\OutboxLOG.txt";
string outputPath = @"C:\FAXLOG\OutboxLOG.txt";

string content = File.ReadAllText(inputPath, Encoding.Unicode);
File.WriteAllText(outputPath, content, Encoding.UTF8);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

The original file is probably encoded in Unicode (16 bit)

Try reading it like this:

  File.ReadAllText(@"C:\Documents and Settings\All Users\Application Data\Microsoft\Windows NT\MSFax\ActivityLog\OutboxLOG.txt",Encoding.Unicode);
Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
1

File.WriteAllText is certainly not so egregiously buggy; if it were, people would have already noticed.

The immediate problem here is that ReadAllText does not correctly detect the encoding of your input file. This method is documented to detect encodings based on the presence of BOMs, and the documentation says that encoding formats UTF-8 and UTF-32 (both big-endian and little-endian) can be detected.

The underlying issue is that you cannot simply treat files as "text" today, and detection is not very reliable and does not always work; for guaranteed results you also need to know the encoding used. Call the other overload of ReadAllText, specifying the correct encoding parameter, and the problem will be solved.

Jon
  • 428,835
  • 81
  • 738
  • 806
0

If you're just copying a file, use File.Copy instead.

That being said, this sounds like an encoding issue. Try using the File.ReadAllText method overload that includes the second argument, which specifies encoding. Make sure you're using the same encoding all the way through your process.

djdanlib
  • 21,449
  • 1
  • 20
  • 29
0

Why not use ReadAllLines would that work for you instead of read all text

MethodMan
  • 18,625
  • 6
  • 34
  • 52
0

Try this:

string content = File.ReadAllText(@"C:\Documents and Settings\All Users\Application Data\Microsoft\Windows NT\MSFax\ActivityLog\OutboxLOG.txt",
                                  System.Text.Encoding.Unicode);
dgvid
  • 26,293
  • 5
  • 40
  • 57