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.
public static System.IO.FileStream Create (string path, int bufferSize, System.IO.FileOptions options);
Parameter: This function accepts three parameters which are illustrated below:
- path: This is the specified file path.
- bufferSize: This is the specified buffer size.
- options: This is one of the FileOptions values that describes how to create or overwrite the file.
- 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 specified 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 path is in an invalid format.
Return Value: Returns a new file with the specified buffer size.
Below are the programs to illustrate the File.Create(String, Int32, FileOptions) method.
Program 1: Initially, no file is created. But below code itself crate a new file file.txt with the specified contents.
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.
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.Create(String, Int32, FileOptions, FileSecurity) Method in C# with Examples
- Array.BinarySearch(Array, Int32, Int32, Object) Method with examples in C#
- C# | Buffer.BlockCopy(Array, Int32, Array, Int32, Int32) Method
- Int32.GetHashCode Method in C# with Examples
- Int32.Equals Method in C# with Examples
- Int32.GetTypeCode Method in C# with Examples
- Int32.CompareTo Method in C# with Examples
- Int32.Parse(String) Method in C# with Examples
- C# | Char.GetUnicodeCategory(String, Int32) Method with Examples
- File.Create(String, Int32) Method in C# with Examples
- C# | Array.BinarySearch(Array, Int32, Int32, Object, IComparer) Method
- Int32.MinValue Field in C# with Examples
- Int32.MaxValue Field in C# with Examples
- C# | Int32.ToString Method | Set - 2
- C# | Int32.ToString Method | Set - 1
- C# | Char.ConvertFromUtf32(Int32) Method
- C# | Char.IsSurrogate(String, Int32) Method
- C# | Char.IsLowSurrogate(String, Int32) Method
- C# | Char.IsControl(String, Int32) Method
- C# | Char.IsSurrogatePair(String, Int32) Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.