In Focus

Use C# Write To A File

In this article, I’ll explain how to write to a file in C# using the StreamWriter and File classes.

There are several ways to write text to a file in C#. Two common ways are to use the StreamWriter and File classes.
 

Write to a file using C# StreamWriter Class

 
C# StreamWriter class is one of the common ways to create and write to a file. The StreamWriter constructor takes a full file name and creates the file if it does not exist. The following code example uses a StreamWriter to create and write to a file. The WriteLine method of the StreamWriter writes the next line in the file. 
  1. // Write file using StreamWriter  
  2. using (StreamWriter writer = new StreamWriter(fullPath))  
  3. {  
  4.    writer.WriteLine("Monica Rathbun");  
  5.    writer.WriteLine("Vidya Agarwal");  
  6.    writer.WriteLine("Mahesh Chand");  
  7.    writer.WriteLine("Vijay Anand");  
  8.    writer.WriteLine("Jignesh Trivedi");  
  9. }  
  10. // Read a file  
  11. string readText = File.ReadAllText(fullPath);  
  12. Console.WriteLine(readText);  
 

Write to a file using C# File Class

 
The File.WriteAllLines method writes a string array to a file. If the file is not created, it creates a new file. If the file is already created, it will overwrite the existing file. Once file writing is done, it closes the file. An exception occurs if the file is readonly, the file name is too long, or the disk is full. Alternatively, you can also specify the Encoding.
 
The following code snippet uses the File.WriteAllLines method to write an array of strings to a text file. 
  1. // Folder, where a file is created.  
  2. // Make sure to change this folder to your own folder  
  3. string folder = @"C:\Temp\";  
  4. // Filename  
  5. string fileName = "CSharpCornerAuthors.txt";  
  6. // Fullpath. You can direct hardcode it if you like.  
  7. string fullPath = folder + fileName;  
  8. // An array of strings  
  9. string[] authors = {"Mahesh Chand""Allen O'Neill""David McCarter",  
  10. "Raj Kumar""Dhananjay Kumar"};  
  11. // Write array of strings to a file using WriteAllLines.  
  12. // If the file does not exists, it will create a new file.  
  13. // This method automatically opens the file, writes to it, and closes file  
  14. File.WriteAllLines(fullPath, authors);  
  15. // Read a file  
  16. string readText = File.ReadAllText(fullPath);  
  17. Console.WriteLine(readText);  
The File.WriteAllText method writes a string to a file. Similar to the File.WriteAllLines method, the File.WriteAllText creates a new file if the file is not created. If the file is already created, it will overwrite the existing file. Once file writing is done, it closes the file. An exception occurs if the file is readonly, the file name is too long, or the disk is full. Alternatively, you can also specify the Encoding.
 
The following code snippet uses the File.WriteAllText method to write a strings to a text file. 
  1. string someText = "C# Corner is a community of software and data developers";  
  2. File.WriteAllText(@"C:\Temp\csc.txt", someText);  
  3. // Read a file  
  4. string readText = File.ReadAllText(@"C:\Temp\csc.txt");  
  5. Console.WriteLine(readText);  
The File.WriteAllBytes method writes a byte array to a file and closes the file. If the target file already exists, it is overwritten.
 

Further Recommended Reading

 
Here are more articles related to Files in C#.