How to write to a text file in C#

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

Write to a text file in C# using StreamWriter

C# StreamWriter class is one of the common ways to create and write 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. 

// Write file using StreamWriter  
using (StreamWriter writer = new StreamWriter(fullPath))  
{  
   writer.WriteLine("Monica Rathbun");  
   writer.WriteLine("Vidya Agarwal");  
   writer.WriteLine("Mahesh Chand");  
   writer.WriteLine("Vijay Anand");  
   writer.WriteLine("Jignesh Trivedi");  
}  
// Read a file  
string readText = File.ReadAllText(fullPath);  
Console.WriteLine(readText);

Learn more here, C# StreamWriter Example (c-sharpcorner.com)

Write to a text file in C# using the 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. 

// Folder, where a file is created.  
// Make sure to change this folder to your own folder  
string folder = @"C:\Temp\";  
// Filename  
string fileName = "CSharpCornerAuthors.txt";  
// Fullpath. You can direct hardcode it if you like.  
string fullPath = folder + fileName;  
// An array of strings  
string[] authors = {"Mahesh Chand", "Allen O'Neill", "David McCarter",  
"Raj Kumar", "Dhananjay Kumar"};  
// Write array of strings to a file using WriteAllLines.  
// If the file does not exists, it will create a new file.  
// This method automatically opens the file, writes to it, and closes file  
File.WriteAllLines(fullPath, authors);  
// Read a file  
string readText = File.ReadAllText(fullPath);  
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 string to a text file. 

string someText = "C# Corner is a community of software and data developers";  
File.WriteAllText(@"C:\Temp\csc.txt", someText);  
// Read a file  
string readText = File.ReadAllText(@"C:\Temp\csc.txt");  
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.

Summary

This article explained how to write a text file in C#.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.