如何在没有CRLF的情况下在C#中编写所有行 [英] How to WriteAllLines in C# without CRLF

查看:168
本文介绍了如何在没有CRLF的情况下在C#中编写所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#,并试图输出几行到一个ASCII文件。我遇到的问题是我的Linux主机正在看到这些文件:

 带有CRLF行结束符的ASCII文本

我需要这个文件才行:

  ASCII文本

CRLF导致了一些问题,我希望在C#中有一种方法就是以我想要的方式创建文件格式。



我基本上使用这个代码:

  string [] lines = {Line1,Line2}; 
File.WriteAllLines(myOutputFile,lines,System.Text.Encoding.UTF8);

有没有简单的方法来创建没有CRLF行结束符的文件?我可以在Linux端处理它,但是宁愿只是从一开始就以适当的格式创建文件。 解决方案

假设你仍然想要换行符,你只需要换行,而不是回车/换行符,你可以使用:

  File.WriteAllText(myOutputFile,string.Join(\\\
,lines));

或者如果您确实需要在最后一行之后换行:

  File.WriteAllText(myOutputFile,string.Join(\\\
,lines)+\\\
);

(另外,正如你所说,你可以在Linux端修正它,例如 dos2unix 。)


I'm using C# and am trying to output a few lines to an ASCII file. The issue I'm having is that my Linux host is seeing these files as:

ASCII text, with CRLF line terminators

I need this file to be just:

ASCII text

The CRLF is causing some issues and I was hoping there was a way in C# to just create the file formatted in the way I want.

I'm basically using this code:

string[] lines = { "Line1", "Line2" };
File.WriteAllLines(myOutputFile, lines, System.Text.Encoding.UTF8);

Is there an easy way to create the file without the CRLF line terminators? I can probably take care of it on the Linux side, but would rather just create the file in the proper format from the beginning.

解决方案

Assuming you still actually want the linebreaks, you just want line feeds instead of carriage return / line feed, you could use:

File.WriteAllText(myOutputFile, string.Join("\n", lines));

or if you definitely want a line break after the last line too:

File.WriteAllText(myOutputFile, string.Join("\n", lines) + "\n");

(Alternatively, as you say, you could fix it on the Linux side, e.g. with dos2unix.)

这篇关于如何在没有CRLF的情况下在C#中编写所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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