File.Create(String) Method in C# with Examples
Last Updated :
28 Apr, 2025
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:
- path: This is the specified file path.
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 as defined by InvalidPathChars.
- ArgumentNullException: The path is null.
- PathTooLongException: The given path, file name, or both exceed the system-defined maximum length.
- DirectoryNotFoundException: The given path is invalid.
- IOException: An I/O error occurred while creating the file.
- NotSupportedException: The given path is in an invalid format.
Below are the programs to illustrate the File.Create(String) method.
Program 1: Initially, no file is created. Below code itself create a new file file.txt with the specified contents.
C#
// C# program to illustrate the usage
// of File.Create(String) method
// Using System, System.IO and
// System.Text namespaces
using System;
using System.IO;
using System.Text;
class GFG {
public static void Main()
{
// Specifying a file path
string file_path = @"file.txt";
try {
// Creating a new file, or overwrite
// if the file already exists.
using(FileStream fs = File.Create(file_path))
{
// Adding some info into the file
byte[] info = new UTF8Encoding(true).GetBytes("GeeksforGeeks");
fs.Write(info, 0, info.Length);
}
// Reading the file contents
using(StreamReader sr = File.OpenText(file_path))
{
string s = "";
while ((s = sr.ReadLine()) != null) {
Console.WriteLine(s);
}
}
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
}
Executing:
mcs -out:main.exe main.cs
mono main.exe
GeeksforGeeks
After running the above code, above output is shown and a new file file.txt is created with some specified contents shown below:
Program 2: The below shown file file.txt is created before running the below code.
C#
// C# program to illustrate the usage
// of File.Create(String) method
// Using System, System.IO and
// System.Text namespaces
using System;
using System.IO;
using System.Text;
class GFG {
public static void Main()
{
// Specifying a file path
string file_path = @"file.txt";
try {
// Overwriting the above file contents
using(FileStream fs = File.Create(file_path))
{
// Adding some info into the file
byte[] info = new UTF8Encoding(true).GetBytes("GFG is a CS Portal");
fs.Write(info, 0, info.Length);
}
// Reading the file contents
using(StreamReader sr = File.OpenText(file_path))
{
string s = "";
while ((s = sr.ReadLine()) != null) {
Console.WriteLine(s);
}
}
}
catch (Exception ex) {
Console.WriteLine(ex.ToString());
}
}
}
Executing:
mcs -out:main.exe main.cs
mono main.exe
GFG is a CS Portal
After running the above code, the above output is shown, and existing file contents get overwritten.
Similar Reads
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.CreateText() Method in C# with Examples 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 Crea
3 min read
File.Copy(String, String) Method in C# with Examples File.Copy(String, String) is an inbuilt File class method that is used to copy the content of the existing source file content to another destination file which is created by this function. Syntax:  public static void Copy (string sourceFileName, string destFileName); Parameter: This function acce
3 min read
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, 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