Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

file io - How to WriteAllLines in C# without CRLF

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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("
", lines));

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...