Read and write content of file c#



In the following example, we use the WriteAllText() method to create a file named "filename.txt" and write some content to it. Then we use the ReadAllText() method to read the contents of the file:

  1. using System.IO;  // include the System.IO namespace  
  2.   
  3. string writeText = "Hello World!";  // Create a text string  
  4. File.WriteAllText("filename.txt", writeText);  // Create a file and write the content of writeText to it  
  5.   
  6. string readText = File.ReadAllText("filename.txt");  // Read the contents of the file  
  7. Console.WriteLine(readText);  // Output the content    

0 Comment's

Comment Form

Submit Comment