Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(C# WinFoms) Writing files and New line

I'm making changes to a file, the 'source' file is a plain text file, but from a UNIX system.

I'm using a StreamReader to read the 'source' file, I then make and store the changes to a StringBuilder using AppendLine().

I then create a StreamWriter (set-up with the same Encoding as the StreamReader) and write the file.

Now when I compare the two files using WinMerge it reports that the carriage return characters are different.

What should I do to ensure that the carriage return characters are the same for the type of file that I am editing?

It should also be noted that the files that are to be modified could come from any system, not just from a UNIX system, they could equally be from a Windows system - so I want a way of inserting these new lines with the same type of carriage return as the original file

Thanks very much for any and all replies.

:)

like image 414
JustAPleb Avatar asked Dec 10 '25 07:12

JustAPleb


1 Answers

First you need to assert what is the newline character for the current file. This can be a problem since a given file can mix different line endings.

Then you can set the NewLine property of your StreamWriter:

StreamWriter sw = new StreamWriter("example.txt");

sw.NewLine = "\r";

For an interesting read about line endings check out The Great Newline Schism.

like image 146
João Angelo Avatar answered Dec 12 '25 20:12

João Angelo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!