StreamWriter 在行尾添加一个额外的 \r

StreamWriter add an extra \r in the end of the line

我创建了一个class,负责生成一个文本文件,其中每一行代表'MyDataClass'class的一个对象的信息。下面是我的代码的简化:

public class Generator
{
    private readonly Stream _stream;
    private readonly StreamWriter _streamWriter;
    private readonly List<MyDataClass> _items;

    public Generator(Stream stream)
    {
        _stream = stream;
        _streamWriter = new StreamWriter(_stream, Encoding.GetEncoding("ISO-8859-1"));
    }

    public void Generate()
    {
        foreach (var item in _items)
        {
            var line = AnotherClass.GetLineFrom(item);

            _streamWriter.WriteLine(line);
        }

        _streamWriter.Flush();
        _stream.Position = 0;
    }
}

我这样称呼 class:

using (var file = new FileStream("name", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
    new Generator(file).Generate();
}

当我 运行 visual studio 上的应用程序时(我使用 运行 (Ctrl+F5)、调试 (F5)、调试和发布模式进行测试)全部按照计划。但是我在 IIS 服务器上发布了应用程序,现在 StreamWriter class 在行尾之前放置了一个额外的 \r 。

查看两个生成文件的十六进制读数:

运行 在 Visual Studio 中: http://www.jonataspiazzi.xpg.com.br/hex_vs.bmp

运行 在 IIS 中: http://www.jonataspiazzi.xpg.com.br/hex_iis.bmp

一些我已经检查过的东西:

Write the line variable (in var line = AnotherClass.GetLineFrom(item);) in a log to see if an extra '\r' is uncluded by the class AnotherClass.

没有结果,line 中的最后一个字符是预期的常规字符(在上面的示例中是 space)。

Write another code to see if the problem is general for all IIS StreamWriter instances.

我试过这个:

var ms = new MemoryStream();
var sw = new StreamWriter(ms, Encoding.GetEncoding("ISO-8859-1"));

sw.WriteLine("Test");
sw.WriteLine("Of");
sw.WriteLine("Lines");

sw.Flush();
ms.Position = 0;

在这种情况下,代码适用于 visual studio 和 IIS。

我在这里待了 3 天,我已经尝试了我的大脑能想到的一切。有人知道我可以尝试什么吗?

更新

变得更奇怪!我尝试将行 _streamWriter.WriteLine(line); 替换为:

_streamWriter.Write(linhaTexto + Environment.NewLine);

更糟糕的是:

_streamWriter.Write(linhaTexto + "\r\n");

两者都不断生成额外的 \r 字符。

我尝试用这个替换:

_streamWriter.Write(linhaTexto + "#\r\n#");

并得到:

http://www.jonataspiazzi.xpg.com.br/hex_sharp.bmp

根据 MSDN,WriteLine

Writes data followed by a line terminator to the text string or stream.

你的最后一行应该是

 _streamWriter.Write(line);

将它放在循环之外并更改循环,使其不管理最后一行。

我遇到了类似的问题。 Environment.NewLine 和 WriteLine 给了我额外的 \r 字符。但下面这个对我有用:

StringBuilder sbFileContent = new StringBuilder();
sbFileContent.Append(line);
sbFileContent.Append("\n");
streamWriter.Write(sbFileContent.ToString());

我的猜测是在 FTP 期间添加了额外的 \r (也许尝试二进制传输)

喜欢here

我已经测试了代码,额外的 /r 不是由于当前问题中的代码造成的

我刚才遇到了类似的问题,下面的代码 会在输出文件 (outFile)

中随机插入空行
using (StreamWriter outFile = new StreamWriter(outFilePath, true)) {
     foreach (string line in File.ReadLines(logPath)) {
            string concatLine = parse(line, out bool shouldWrite);
            if (shouldWrite) {
              outFile.WriteLine(concatLine);       
            }     
      }   
}

利用 Antar 的想法,我更改了我的解析函数,以便它返回附加了 Environment.NewLine 的行,即

return myStringBuilder.Append(Environment.NewLine).ToString();

然后在上面的 foreach 循环中,更改了

outFile.WriteLine(concatLine);

outFile.Write(concatLine);

现在它写入文件时没有插入一堆随机的新行。但是,我仍然完全不知道为什么我必须这样做。