The shortest / shortest way to add a new line to a file

There are various ways to add text to files, although I was wondering if anyone knows the shortest way to do this.

eg. Add a new line to the existing txt file:

line1 line2 <new-line-here> 
+6
source share
5 answers

It will be something like:

 File.AppendAllText("c:\filepath.txt", "text to append"); 

See File.AppendAllText for details . The File class contains many useful static methods for performing common file operations.

+12
source

The static methods of the File class do this fairly straightforward:

 File.AppendAllLines("filename.txt", new string[] { "text to append" }); 

Edit: Using an array is a little shorter.

+5
source
 System.IO.File.AppendAllText("some file", Environment.NewLine); 

Is that what you mean?

+3
source

Try this code

 StreamWriter sw = File.AppendText(file_path); sw.WriteLine("appended text"); 

Source = http://msdn.microsoft.com/en-us/library/system.io.file.appendtext.aspx

+2
source
  System.IO.File.AppendAllText(@"c:\test.txt",Environment.NewLine); 

The right way to do this, just \ n don't do this!

+2
source

Source: https://habr.com/ru/post/903983/


All Articles