Create a Text File in C#
The following 4 methods can be used to create a text file in C#. In this article and code sample, we will learn how to use these methods to create text files.
- File.Create
- File.CreateText
- FileInfo.Create
- FileInfo.CreateText
File.Create Method
The File.Create() method takes a file name with the full path as its first and required parameter and creates a file at the specified location. If same file already exists at the same location, this method overwrites the file.
The following code snippet creates a file Mahesh.txt in C:\Temp folder. If file already exists, the code will delete the existing file. The code writes two arrays of bytes to the file.
The Create method creates and returns a FileStream object that is responsible for reading and writing the specified file.
string fileName = @"C:\Temp\Mahesh.txt";
try
{
// Check if file already exists. If yes, delete it.
if (File.Exists(fileName))
{
File.Delete(fileName);
}
// Create a new file
using (FileStream fs = File.Create(fileName))
{
// Add some text to file
Byte[] title = new UTF8Encoding(true).GetBytes("New Text File");
fs.Write(title, 0, title.Length);
byte[] author = new UTF8Encoding(true).GetBytes("Mahesh Chand");
fs.Write(author, 0, author.Length);
}
// Open the stream and read it back.
using (StreamReader sr = File.OpenText(fileName))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
Console.WriteLine(s);
}
}
}
catch (Exception Ex)
{
Console.WriteLine(Ex.ToString());
}
The Create method has four overloaded forms provide options with a file buffer size, file options, and file security.
Create File with Buffer Size
File.Create(name of the file, number of bytes buffered for read and write to the file)
FileStream fs = File.Create(fileName, 1024);
Create File with File Options
The File.Create method takes third parameters as a FileOptions enumeration that can be used to specify advanced options for creating a FileStream object.
FileStream fs = File.Create(fileName, 1024, FileOptions.WriteThrough);
Create File with File Security
The Create method also has an option to specify the file security options. The fourth parameter passed within the Create method of type FileSecurity object.
try
{
string fileName = @"C:\Temp\Mahesh2.txt";
// Create File Security
FileSecurity fSecurity = new FileSecurity();
fSecurity.AddAccessRule(new FileSystemAccessRule(@"DomainName\AccountName", FileSystemRights.ReadData, AccessControlType.Allow));
using (FileStream fs = File.Create(fileName, 1024, FileOptions.WriteThrough, fSecurity))
{
// Add some text to file
Byte[] title = new UTF8Encoding(true).GetBytes("New Text File");
fs.Write(title, 0, title.Length);
byte[] author = new UTF8Encoding(true).GetBytes("Mahesh Chand");
fs.Write(author, 0, author.Length);
}
Console.WriteLine("Adding access control entry for " + fileName);
Console.WriteLine("Done.");
}
catch (Exception e)
{
Console.WriteLine(e);
}
Renzo Loza MuñozPosted Jun 6, 2020, 3:13 PM
Awesome!!!!!!!
Mohammad HasanPosted Dec 19, 2019, 7:57 PM
Simple and nice
Rushi MehtaPosted Dec 28, 2018, 5:59 AM
Nice Article Mahesh..
anup kadamPosted Dec 27, 2018, 11:53 PM
Well written article
Rushi MehtaPosted Nov 20, 2018, 3:21 AM
Nice Article
Rumbidzai MuserepwaPosted Sep 5, 2016, 9:09 AM
Nice article thanks Mahesh
Rajib Das GuptaPosted Jun 20, 2016, 3:53 AM
Nice