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 itself creates a new file.

Syntax:

public static System.IO.FileStream Create (string path, int bufferSize, System.IO.FileOptions options, System.Security.AccessControl.FileSecurity fileSecurity);

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.
  • fileSecurity: This is a object that determines the access control and audit security for the file.

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 specified path, file name, or both exceed the system-defined maximum length.
  • DirectoryNotFoundException: The specified 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, file options, and file security.

Below are the programs to illustrate the File.Create(String, Int32, FileOptions, FileSecurity) method.

Program 1: Initially, no file is created. But below code itself crate a new file file.txt with the specified contents.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the usage
// of File.Create(String, Int32, 
// FileOptions, FileSecurity) method
  
// Using System, System.IO, System.Text and
// System.Security.AccessControl namespaces
using System;
using System.IO;
using System.Text;
using System.Security.AccessControl;
  
class GFG {
    public static void Main()
    {
        // Specifing a file
        string myfile = @"file.txt";
  
        // Calling the create() function to create a
        // new file named as file.txt
        using(FileStream fs = File.Create(myfile, 1024, 
            FileOptions.RandomAccess, new FileSecurity()))
        {
            // Adding the below contents
            Byte[] info = new UTF8Encoding(true).GetBytes("GeeksforGeeks");
            fs.Write(info, 0, info.Length);
        }
  
        // Reading the file contents
        using(StreamReader sr = File.OpenText(myfile))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) {
                Console.WriteLine(s);
            }
        }
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the usage
// of File.Create(String, Int32, 
// FileOptions, FileSecurity) method
  
// Using System, System.IO, System.Text and
// System.Security.AccessControl namespaces
using System;
using System.IO;
using System.Text;
using System.Security.AccessControl;
  
class GFG {
    public static void Main()
    {
        // Specifing a file
        string myfile = @"file.txt";
  
        // Calling the create() function
        using(FileStream fs = File.Create(myfile, 1024,
             FileOptions.RandomAccess, new FileSecurity()))
        {
            // Overwriting the above file with
            // below contents
            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(myfile))
        {
            string s = "";
            while ((s = sr.ReadLine()) != null) {
                Console.WriteLine(s);
            }
        }
    }
}

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.