Introducing Radical.sh

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

Writing into a file using WriteAllText() in C#

We can use File.WriteAllText(Filepath) for writing the content to a file.
Remember, the previous content will be no more and the new content will be written by the File.WriteAllText().

  1. using System;
  2. using System.IO;
  3.  
  4. namespace forgetCode
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. string path = "D:\\test.txt";
  11. if (File.Exists(path))
  12. {
  13. Console.WriteLine("Please enter new content for the file:");
  14. string newContent = Console.ReadLine();
  15. File.WriteAllText(path, newContent);
  16. }
  17.  
  18. }
  19. }
  20. }
  21.  


Output:
The content will be written newly (Deleting previous content) to the mentioned path.

..