简体   繁体   中英

C#: WriteFile with Line Breaks

I have several web site files that I have to edit systematically. Therefore, I have written a small program that changes certain lines. I read the content with

List<string> list = new List<string>(
                        File.ReadAllLines(fileName,
                                          Encoding.GetEncoding(1252))
                                    );

make the changes, and save the content with

System.IO.StreamWriter file = new System.IO.StreamWriter(fileName, false, Encoding.GetEncoding(1252));
file.WriteLine(content);

Sadly, this saves the content in one single line without line breaks. Could you please tell me which other function I have to use or how I have to adjust the code so the original line breaks don't get lost in the process of writing the file?

Try to use File.WriteAllLines method instead:

As per MSDN:

Creates a new file, writes one or more strings to the file, and then closes the file.

Code:

List<string> list = new List<string>(
                    File.ReadAllLines(fileName,
                                      Encoding.GetEncoding(1252))
                                );
// ... do something (editing) ... 
File.WriteAllLines(fileName, list);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM