Open In App

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 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..

Exceptions:
 



Return Value: Returns an unshared FileStream that provides access to the specified file, with the specified mode and access.
Below are the programs to illustrate the File.Open(String, FileMode, FileAccess) method.
Program 1: Below code creates a temporary file, writes some specified contents into it, open that file and print its contents.
 




// C# program to illustrate the usage
// of File.Open(String, FileMode,
// FileAccess) 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))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
 
            while (fs.Read(b, 0, b.Length) > 0) {
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}

Output: 
 

GFG is a CS Portal.

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

This below code will open the file file.txt and will print its contents.
 




// C# program to illustrate the usage
// of File.Open(String, FileMode,
// FileAccess) 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))
        {
            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));
            }
        }
    }
}

Output: 
 

GeeksforGeeks

 


Article Tags :
C#