3

File.WriteAllText 在每个字母和引号后插入一个空格。

例子:

原始文件

"JobID" "ParentJobID"

新文件

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

代码

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

6 回答 6

8

我不认为是WriteAllText这样做的。我相信它是ReadAllText,它默认使用 UTF-8 读取 - 我怀疑您的OutboxLOG.txt文件实际上是用 UTF-16 编写的。试试这个:

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);
于 2011-12-21T14:52:41.710 回答
1

原始文件可能以 Unicode(16 位)编码

尝试像这样阅读它:

  File.ReadAllText(@"C:\Documents and Settings\All Users\Application Data\Microsoft\Windows NT\MSFax\ActivityLog\OutboxLOG.txt",Encoding.Unicode);
于 2011-12-21T14:52:09.793 回答
1

File.WriteAllText肯定不是那么严重的越野车;如果是的话,人们早就注意到了。

这里的直接问题是ReadAllText无法正确检测输入文件的编码。这种方法被记录为根据 BOM 的存在来检测编码,并且文档说可以检测编码格式 UTF-8 和 UTF-32(大端和小端)。

根本问题是您今天不能简单地将文件视为“文本”,并且检测不是很可靠并且并不总是有效;为了保证结果,您还需要知道使用的编码。调用另一个重载ReadAllText,指定正确的编码参数,问题就解决了。

于 2011-12-21T14:55:53.237 回答
0

如果您只是复制文件,请改用 File.Copy。

话虽如此,这听起来像是一个编码问题。尝试使用包含指定编码的第二个参数的 File.ReadAllText 方法重载。确保您在整个过程中一直使用相同的编码。

于 2011-12-21T14:53:12.943 回答
0

为什么不使用 ReadAllLines 会为您工作而不是阅读所有文本

于 2011-12-21T15:01:11.493 回答
0

试试这个:

string content = File.ReadAllText(@"C:\Documents and Settings\All Users\Application Data\Microsoft\Windows NT\MSFax\ActivityLog\OutboxLOG.txt",
                                  System.Text.Encoding.Unicode);
于 2011-12-21T15:01:21.453 回答