Open In App

File.Open(String, FileMode, FileAccess, FileShare) Method in C# with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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 access, System.IO.FileShare share); 
 

Parameter: This function accepts three parameters which are illustrated below: 
 

  • path: This is the specified file to open.
  • mode: This mode value specifies whether a new file is created if one does not exist, and also determines whether the existing file’s contents are retained or overwritten.
  • access: This value specifies the operations that can be performed on the file.
  • share: This value specifying the type of access other threads have to the file.

Exceptions:
 

  • ArgumentException: The path is a zero-length string, contains only white space, or one or more invalid characters as defined by InvalidPathChars. OR access specified Read and mode specified Create, CreateNew, Truncate, or Append.
  • 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 opening the file.
  • UnauthorizedAccessException: The path specified a file that is read-only and access is not Read. OR path specified a directory. OR the caller does not have the required permission. OR mode is Create and the specified file is a hidden file.
  • ArgumentOutOfRangeException: The mode, access, or share specified an invalid value.
  • FileNotFoundException: The file specified in the path was not found.
  • NotSupportedException: The path is in an invalid format.

Return Value: Returns a FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.
Below are the programs to illustrate the File.Open(String, FileMode, FileAccess, FileShare) method.
Program 1: Below code creates a temporary file, writes some specified contents into it, open that file and print its contents. Here file sharing is not allowed.
 

CSharp




// C# program to illustrate the usage
// of File.Open(String, FileMode,
// FileAccess, FileShare) method
 
// Using System, System.IO and
// System.Text namespaces
using System;
using System.IO;
using System.Text;
 
class GFG {
    public static void Main()
    {
        // Creating a temporary file
        string path = Path.GetTempFileName();
        using(FileStream fs = File.Open(path, FileMode.Open))
        {
            // Putting some contents
            Byte[] info = new UTF8Encoding(true).GetBytes("GFG is a CS Portal.");
            fs.Write(info, 0, info.Length);
        }
 
        // Opening the stream and reading it back.
        using(FileStream fs = File.Open(path, FileMode.Open,
                           FileAccess.Read, FileShare.None))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
 
            while (fs.Read(b, 0, b.Length) > 0) {
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}


Executing: 
 

GFG is a CS Portal.

Program 2: Initially, a file file.txt is created with some contents shown below:
 

file.txt

This code will open the file file.txt and will print its contents with file sharing is not allowed.
 

CSharp




// C# program to illustrate the usage
// of File.Open(String, FileMode,
// FileAccess, FileShare) 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 path = @"file.txt";
 
        // Opening above file and reading it back.
        using(FileStream fs = File.Open(path, FileMode.Open,
                           FileAccess.Read, FileShare.None))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
 
            while (fs.Read(b, 0, b.Length) > 0) {
                // Printing the file contents
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}


Executing: 
 

GeeksforGeeks

 



Last Updated : 13 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads