File.CreateText() Method in C# with Examples Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report File.CreateText() is an inbuilt File class method that is used to overwrite the contents of the existing file with the given UTF-8 encoded text and if the file is not created already, this function will create a new file with the specified contents. Syntax: public static System.IO.StreamWriter CreateText (string path); Parameter: This function accepts a parameter which is illustrated below: Path: This is the file where UTF-8 encoded texts are going to be overwritten. The file is created if it doesn't already exist. Exceptions: UnauthorizedAccessException: The caller does not have the required permission. OR the path specified a file that is read-only. OR the path specified a file that is hidden.ArgumentException: The path is a zero-length string, contains only white space, or one or more invalid characters.ArgumentNullException: The path is null.PathTooLongException: The specified path, file name, or both exceed the system-defined maximum length.DirectoryNotFoundException: The specified path is invalid i.e, it is on an unmapped drive.NotSupportedException: The path is in an invalid format. Return Value: Returns a StreamWriter that writes to the specified file using UTF-8 encoding.Below are the programs to illustrate the File.CreateText() method. Program 1: Before running the below code, a file file.txt is created with some contents which is shown below: C# // C# program to illustrate the usage // of File.CreateText() method // Using System, System.IO namespaces using System; using System.IO; class GFG { // Main method public static void Main() { // Creating a file string myfile = @"file.txt"; // Overwriting to the above existing file using(StreamWriter sw = File.CreateText(myfile)) { sw.WriteLine("GeeksforGeeks"); sw.WriteLine("is a"); sw.WriteLine("computer science"); sw.WriteLine("portal."); } // Opening the file for reading using(StreamReader sr = File.OpenText(myfile)) { string s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } } } Executing: mcs -out:main.exe main.cs mono main.exe GeeksforGeeks is a computer science portal. After running the above code, above output is shown and the existing file file.txt becomes like below: Program 2: Initially, no file is created and hence below code itself create a file named as file.txt C# // C# program to illustrate the usage // of File.CreateText() method // Using System, System.IO namespaces using System; using System.IO; class GFG { // Main method public static void Main() { // Creating a file string myfile = @"file.txt"; // Checking the existence of above file if (!File.Exists(myfile)) { // Creating a new file with below contents using(StreamWriter sw = File.CreateText(myfile)) { sw.WriteLine("Geeks"); sw.WriteLine("GFG"); sw.WriteLine("GeeksforGeeks"); } } // Opening the file for reading using(StreamReader sr = File.OpenText(myfile)) { string s = ""; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); } } } } Executing: mcs -out:main.exe main.cs mono main.exe Geeks GFG GeeksforGeeks After running the above code, a new file file.txt is created which is shown below: Comment More infoAdvertise with us Next Article File.CreateText() Method in C# with Examples K Kanchan_Ray Follow Improve Article Tags : C# CSharp-File-Handling Similar Reads File.Delete() Method in C# with Examples File.Delete(String) is an inbuilt File class method which is used to delete the specified file.Syntax:Â Â public static void Delete (string path); Parameter: This function accepts a parameter which is illustrated below:Â Â path: This is the specified file path which is to be deleted. Exceptions:Â Argu 3 min read File.Create(String) Method in C# with Examples File.Create(String) is an inbuilt File class method which is used to overwrites an existing file else create a new file if the specified file is not existing. Syntax:Â public static System.IO.FileStream Create (string path); Parameter: This function accepts a parameter which is illustrated below: 3 min read File.AppendText() Method in C# with Examples File.AppendText() is an inbuilt File class method which is used to create a StreamWriter that appends UTF-8 encoded text to an existing file else it creates a new file if the specified file does not exist.Syntax:Â Â public static System.IO.StreamWriter AppendText (string path); Parameter: This functi 3 min read File.Create(String, Int32) Method in C# with Examples File.Create(String, Int32) is an inbuilt File class method which is used to overwrite an existing file, specifying a buffer size else create a new file if the specified file is not existing.Syntax:Â Â public static System.IO.FileStream Create (string path, int bufferSize); Parameter: This function ac 3 min read File.Move() Method in C# with Examples File.Move() is an inbuilt File class method that is used to move a specified file to a new location. This method also provides the option to specify a new file name. Syntax: public static void Move (string sourceFileName, string destFileName); Parameter: This function accepts two parameters which ar 2 min read File.Exists() Method in C# with Examples File.Exists(String) is an inbuilt File class method that is used to determine whether the specified file exists or not. This method returns true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. Also, if the path is null, then this method re 2 min read File.OpenText() Method in C# with Examples File.OpenText(String) is an inbuilt File class method which is used to open an existing UTF-8 encoded text file for reading.Syntax:Â Â public static System.IO.StreamReader OpenText (string path); Parameter: This function accepts a parameter which is illustrated below:Â Â path: This is the specified te 2 min read File.OpenRead() Method in C# with Examples File.OpenRead(String) is an inbuilt File class method which is used to open an existing file for reading.Syntax:Â Â public static System.IO.FileStream OpenRead (string path); Parameter: This function accepts a parameter which is illustrated below:Â Â path: This is the specified file which is going to 2 min read File.OpenWrite() Method in C# with Examples File.OpenWrite(String) is an inbuilt File class method that is used to open an existing file or creates a new file for writing. Syntax:Â public static System.IO.FileStream OpenWrite (string path); Parameter: This function accepts a parameter which is illustrated below:Â path: This is the specified t 3 min read File.Create(String, Int32, FileOptions) Method in C# with Examples File.Create(String, Int32, FileOptions) is an inbuilt File class method that is used to overwrite an existing file, specifying a buffer size and options that describe how to create or overwrite the file else create a new file if the specified file is not existing.Syntax:Â public static System.IO.Fil 3 min read Like