如何在没有CRLF的情况下在C#中编写所有行 [英] How to WriteAllLines in C# without CRLF
问题描述
带有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屋!