-3


sometimes when I open some file, the Visual Studio shows me a dialog about inconsistent line endings but Visual Studio is able to normalize the line endings.
How can I normalize the line endings (Windows CR LF) in the file by myself with using C#?

I read a definition of stored procedure from SQL Server and save it to the file.

File.WriteAllText("procedure.sql", "content");

But when I open the file in Visual Studio it gives a warning about inconsistent line endings. SQL Server Management Studio also shows the same warnings when I want to modify the stored procedure.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2250152
  • 14,658
  • 4
  • 33
  • 57

1 Answers1

1

If you want to normalize to \r\n then this should be enough:

string filename = "somefile.txt";
var lines = File.ReadAllLines(filename);
File.WriteAllLines(filename, lines);

(it works because the ReadAllLines accept both \r, \n or \r\n, and then on write WriteAllLines always uses \r\n).

Note that this won't preserve encoding (for example the presence/absence of the initial BOF for UTF8, or if the file is in Unicode). The file will be written in UTF8 without BOM.

xanatos
  • 109,618
  • 12
  • 197
  • 280