In this tutorial, i’ll show you How to 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 test.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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
string fileName = @"C:\Temp\test.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("csharp examples"); 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)
1 2 3 |
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.
1 2 3 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
try { string fileName = @"C:\Temp\test.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("csharp examples"); fs.Write(author, 0, author.Length); } Console.WriteLine("Adding access control entry for " + fileName); Console.WriteLine("Done."); } catch (Exception e) { Console.WriteLine(e); } |
File.CreateText Method
The File.CreateText method creates and opens a file for writing UTF-8 encoded text. If file already exists, this method opens the file.
The following code snippet creates a file using the CreateText method that returns a StreamWriter object. The WriteLine method of SteamLine can be used to add line text to the object and writes to the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
string fileName = @"C:\Temp\test.txt"; try { // Check if file already exists. If yes, delete it. if (File.Exists(fileName)) { File.Delete(fileName); } // Create a new file using (StreamWriter sw = File.CreateText(fileName)) { sw.WriteLine("New file created: {0}", DateTime.Now.ToString()); sw.WriteLine("Author: csharp examples"); sw.WriteLine("Add one more line "); sw.WriteLine("Add one more line "); sw.WriteLine("Done! "); } // Write file contents on console. using (StreamReader sr = File.OpenText(fileName)) { string s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } } catch (Exception Ex) { Console.WriteLine(Ex.ToString()); } |
FileInfo.Create Method
The FileInfo.Create method creates a file.
The following code snippet creates a file using the Create method that returns a FileSteam object. The Write method of FileStream can be used to write text to the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
string fileName = @"C:\Temp\test.txt"; FileInfo fi = new FileInfo(fileName); try { // Check if file already exists. If yes, delete it. if (fi.Exists) { fi.Delete(); } // Create a new file using (FileStream fs = fi.Create()) { Byte[] txt = new UTF8Encoding(true).GetBytes("New file."); fs.Write(txt, 0, txt.Length); Byte[] author = new UTF8Encoding(true).GetBytes("Author csharp examples"); fs.Write(author, 0, author.Length); } // Write file contents on console. using (StreamReader sr = File.OpenText(fileName)) { string s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } } catch (Exception Ex) { Console.WriteLine(Ex.ToString()); } |
FileInfo.CreateText Method
The FileInfo.CreateText method creates and opens a file for writing UTF-8 encoded text. If file already exists, this method opens the file.
The following code snippet creates a file using the CreateText method that returns a StreamWriter object. The WriteLine method of SteamLine can be used to add line text to the object and writes to the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
string fileName = @"C:\Temp\test.txt"; FileInfo fi = new FileInfo(fileName); try { // Check if file already exists. If yes, delete it. if (fi.Exists) { fi.Delete(); } // Create a new file using (StreamWriter sw = fi.CreateText()) { sw.WriteLine("New file created: {0}", DateTime.Now.ToString()); sw.WriteLine("Author: Mahesh Chand"); sw.WriteLine("Add one more line "); sw.WriteLine("Add one more line "); sw.WriteLine("Done! "); } // Write file contents on console. using (StreamReader sr = File.OpenText(fileName)) { string s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } } catch (Exception Ex) { Console.WriteLine(Ex.ToString()); } |