Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Appending text to a file in C#

You can append text to a existing file by the following example.
If the file does not exist then the file will be generated and the text will be written.

  1. using System;
  2. using System.IO;
  3.  
  4. namespace forgetCode
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. Console.WriteLine("Please enter the content to append in to the file - type exit and press enter to finish editing:");
  11. string newContent = Console.ReadLine();
  12. while (newContent != "exit")
  13. {
  14. File.AppendAllText("D:\\test.txt", newContent);
  15. newContent = Console.ReadLine();
  16. }
  17.  
  18. }
  19. }
  20. }


Output:
The entered text will be written into a file in the path - D:\\test.txt

..