Introducing Radical.sh

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

Viewing and updating file content in C#

We can view the content of a file and update it with new content.

  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. string content = File.ReadAllText(path);
  14. Console.WriteLine("Current content of file:");
  15. Console.WriteLine(content);
  16. }
  17.  
  18. Console.WriteLine("\nPlease enter the new content");
  19. string newContent = Console.ReadLine();
  20.  
  21. File.WriteAllText(path, newContent);
  22.  
  23. }
  24. }
  25. }


Output:
The content of file will be shown.
The user can enter new content and it will be written to the same file.

..