The Wayback Machine - https://web.archive.org/web/20241001120524/https://www.geeksforgeeks.org/file-createstring-method-in-c-sharp-with-examples/
Open In App

File.Create(String) Method in C# with Examples

Last Updated : 17 Jun, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:
 

file.txt

Program 2: The below shown file file.txt is created before running the below code. 
 

file.txt

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.
 

file.txt

 



Similar Reads

File.Replace(String, String, String) Method in C# with Examples
File.Replace(String, String, String) is an inbuilt File class method that is used to replace the contents of a specified destination file with the contents of a source file then it deletes the source file and creates a backup of the replaced file.Syntax: public static void Replace (string sourceFileName, string destinationFileName, string destinati
3 min read
File.Replace(String, String, String, Boolean) Method in C# with Examples
File.Replace(String, String, String, Boolean) is an inbuilt File class method that is used to replace the contents of a specified destination file with the contents of a source file then it deletes the source file, creates a backup of the replaced file, and optionally ignores merge errors. Syntax: public static void Replace (string sourceFileName,
3 min read
File.Create(String, Int32, FileOptions, FileSecurity) Method in C# with Examples
File.Create(String, Int32, FileOptions, FileSecurity) is an inbuilt File class method which is used to overwrite an existing file, specifying a buffer size and options that describe how to create or overwrite the file and value that determines the access control and audit security for the file. If the specified file is not existing, this function i
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 accepts two parameters which are illustrated below: pa
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.FileStream Create (string path, int bufferSize, System
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 accepts two parameters which are illustrated below: sourc
3 min read
File.AppendAllText(String, String) Method in C# with Examples
File.AppendAllText(String, String) is an inbuilt File class method which is used to append the specified string to the given file if that file exists else creates a new file and then appending is done. It also closes the file.Syntax: public static void AppendAllText (string path, string contents); Parameter: This function accepts two parameters whi
3 min read
File.AppendAllText(String, String, Encoding) Method in C# with Examples
File.AppendAllText(String, String, Encoding) is an inbuilt File class method which is used to append the specified string to the given file using the specified encoding if that file exists else creates a new file and then appending is done. Syntax: public static void AppendAllText (string path, string contents, System.Text.Encoding encoding); Param
3 min read
File.WriteAllText(String, String) Method in C# with Examples
File.WriteAllText(String, String) is an inbuilt File class method that is used to create a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten. Syntax: public static void WriteAllText (string path, string contents); Parameter: This function accepts two parameters which ar
3 min read
File.WriteAllText(String, String, Encoding) Method in C# with Examples
File.WriteAllText(String, String, Encoding) is an inbuilt File class method that is used to create a new file, writes the specified string to the file using the specified encoding, and then closes the file. If the target file already exists, it is overwritten. Syntax: public static void WriteAllText (string path, string contents, System.Text.Encodi
3 min read
File.WriteAllLines(String, String[], Encoding) Method in C# with Examples
File.WriteAllLines(String, String[], Encoding) is an inbuilt File class method that is used to create a new file, writes the specified string array to the file by using the specified encoding, and then closes the file.Syntax: public static void WriteAllLines (string path, string[] contents, System.Text.Encoding encoding); Parameter: This function a
3 min read
File.WriteAllLines(String, String[]) Method in C# with Examples
File.WriteAllLines(String, String[]) is an inbuilt File class method that is used to create a new file, writes the specified string array to the file, and then closes the file.Syntax: public static void WriteAllLines (string path, string[] contents); Parameter: This function accepts two parameters which are illustrated below: path: This is the spec
3 min read
File.WriteAllLines(String, IEnumerable<String>, Encoding) Method in C# with Examples
File.WriteAllLines(String, IEnumerable<String>, Encoding) is an inbuilt File class method that is used to create a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file. Syntax: public static void WriteAllLines (string path, System.Collections.Generic.IEnumerable<String> contents,
3 min read
File.WriteAllLines(String, IEnumerable<String>) Method in C# with Examples
File.WriteAllLines(String, IEnumerable<String>) is an inbuilt File class method that is used to create a new file, writes a collection of strings to the file, and then closes the file. Syntax: public static void WriteAllLines (string path, System.Collections.Generic.IEnumerable<String>contents); Parameter: This function accepts two para
3 min read
File.Copy(String, String, Boolean) Method in C# with Examples
File.Copy(String, String, Boolean) is an inbuilt File class method that is used to copy the content of the existing source file content to another destination file if exist, else create a new destination file then copying process is done.Syntax: public static void Copy (string sourceFileName, string destFileName, bool overwrite); Parameter: This fu
4 min read
File.AppendAllLines(String, IEnumerable<String>, Encoding) Method in C# with Examples
File.AppendAllLines(String, IEnumerable<String>, Encoding) is an inbuilt File class method which is used to append specified lines to a file by using a specified encoding and then closes the file. If the specified file does not exist, this method creates a new file, writes the specified lines to the file, and then closes the file. Syntax: pub
3 min read
File.AppendAllLines(String, IEnumerable<String>) Method in C# with Examples
File.AppendAllLines(String, IEnumerable<String>) is an inbuilt File class method which is used to append specified lines to a file and then closes the file. Syntax: public static void AppendAllLines (string path, System.Collections.Generic.IEnumerable<String> contents); Parameter: This function accepts two parameters which are illustrat
3 min read
File.Open(String, FileMode, FileAccess) Method in C# with Examples
File.Open(String, FileMode, FileAccess) is an inbuilt File class method that is used to open a FileStream on the specified path with the specified mode and access with no sharing.Syntax: public static System.IO.FileStream Open (string path, System.IO.FileMode mode, System.IO.FileAccess access); Parameter: This function accepts three parameters whic
3 min read
File.Open(String, FileMode, FileAccess, FileShare) Method in C# with Examples
File.Open(String, FileMode, FileAccess, FileShare) is an inbuilt File class method that is used to open a FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.Syntax: public static System.IO.FileStream Open (string path, System.IO.FileMode mode, System.IO.FileAccess acce
3 min read
File.ReadAllLines(String) Method in C# with Examples
File.ReadAllLines(String) is an inbuilt File class method that is used to open a text file then reads all lines of the file into a string array and then closes the file.Syntax: public static string[] ReadAllLines (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified file to open for read
2 min read
File.ReadAllLines(String, Encoding) Method in C# with Examples
File.ReadAllLines(String, Encoding) is an inbuilt File class method that is used to open a text file then reads all lines of the file into a string array with the specified encoding and then closes the file.Syntax: public static string[] ReadAllLines (string path, System.Text.Encoding encoding); Parameter: This function accepts two parameters which
3 min read
File.ReadLines(String) Method in C# with Examples
File.ReadLines(String) is an inbuilt File class method that is used to read the lines of a file. Syntax: public static System.Collections.Generic.IEnumerable ReadLines (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified file for reading. Exceptions: ArgumentException: The path is a zer
2 min read
File.ReadLines(String, Encoding) Method in C# with Examples
File.ReadLines(String, Encoding) is an inbuilt File class method that is used to read the lines of a file that has a specified encoding. Syntax: public static System.Collections.Generic.IEnumerable ReadLines (string path, System.Text.Encoding encoding); Parameter: This function accepts two parameters which are illustrated below: path: This is the s
2 min read
File.ReadAllText(String) Method in C# with Examples
File.ReadAllText(String) is an inbuilt File class method that is used to open a text file then reads all the text in the file and then closes the file.Syntax: public static string ReadAllText (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified file to open for reading. Exceptions: Argu
2 min read
File.ReadAllText(String, Encoding) Method in C# with Examples
File.ReadAllText(String, Encoding) is an inbuilt File class method that is used to open a text file then reads all the text in the file with the specified encoding and then closes the file.Syntax: public static string ReadAllText (string path, System.Text.Encoding encoding); Parameter: This function accepts a parameter which is illustrated below: p
2 min read
File.Open(String, FileMode) Method in C# with Examples
File.Open(String, FileMode) is an inbuilt File class method which is used to open a FileStream on the specified path with read/write access with no sharing.Syntax: public static System.IO.FileStream Open (string path, System.IO.FileMode mode); Parameter: This function accepts two parameters which are illustrated below: sourceFileName: This is the s
3 min read
File.GetLastWriteTimeUtc() Method in C# with Examples
File.GetLastWriteTimeUtc(String) is an inbuilt File class method which is used to return the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.Syntax: public static DateTime GetLastWriteTimeUtc (string path); Parameter: This function accepts a parameter which is illustrated below: path: Thi
2 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 are illustrated below: sourceFileName: This is the s
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 returns false. Syntax: public static bool Exists (st
2 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 function accepts a parameter which is illustrated below: p
3 min read
Article Tags :