的StreamWriter在该行的末尾添加一个额外的\ r [英] StreamWriter add an extra \r in the end of the line

查看:211
本文介绍了的StreamWriter在该行的末尾添加一个额外的\ r的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个类负责生成,其中每行再presents的'MyDataClass类对象的信息的文本文件。下面是我的code进行简化:

I created a class with the responsibility to generate a text file where each line represents the information of an object of 'MyDataClass' class. Below is a simplification of my code:

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

和我调用这个类是这样的:

And I call this class like this:

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

当我运行在Visual Studio中的应用程序(我测试与运行(按Ctrl + F5),调试(F5),与调试和释放模式)按计划一切顺利。不过,我出版了IIS服务器的应用程序,现在StreamWriter类把一个额外的\ r行结束之前。

When I run the application on visual studio (I test with run (Ctrl+F5), debug (F5), with debug and release mode) all goes according to the plan. But I publish the application in a IIS server and now StreamWriter class put an extra \r before the end of the line.

检查出来两个生成的文件的十六进制阅读:

Check it out the hexadecimal reading of both generated files:

运行在Visual Studio中:

Running in Visual Studio:

运行在IIS中:

Running in IIS:

我已经查了一些事情:

变量(在 VAR线= AnotherClass.GetLineFrom(项目); )的日志查看是否有额外的'\ r'是由类uncluded AnotherClass

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.

没有造成什么,在的最后一个字符是一个普通的字符像预期的(例如,在上面的空间)。

Didn't result in nothing, the last char in line is a regular char like expected (in example above is a space).

再写code,看看问题是一般所有的IIS的StreamWriter实例。

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;

在这种情况下,code非常适用于这两种Visual Studio和IIS。

In this case the code works well for both visual studio and IIS.

我在这3天,我已经尝试一切我的大脑能想到。有没有人有任何线索什么我可以尝试?

I'm in this for 3 days, I already try everything my brain can think. Did anyone have any clue for what I can try?

更新

获取离奇!我尝试更换行 _streamWriter.WriteLine(线);

Get weirder! I try to replace the line _streamWriter.WriteLine(line); with:

_streamWriter.Write(linhaTexto + Environment.NewLine);

和更糟糕的:

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

这两个保持生成额外的 \ r 字符。

我尝试替换这样的:

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

和获取:

推荐答案

我的猜测是,额外的\ r被FTP过程中添加(也许尝试一个二进制传输)

My guess is that the extra \r is added during FTP (maybe try a binary transfer)

像<一个href="http://superuser.com/questions/165539/winscp-screws-up-line-breaks-in-files-how-do-avoid">here

我测试过的code和额外的/ r是不是由于目前的问题code

I've tested the code and the extra /r is not due to the code in the current question

这篇关于的StreamWriter在该行的末尾添加一个额外的\ r的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆