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

Exceptions:

  • 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 specified path is invalid.
  • IOException: An I/O error occurred while opening the file.
  • UnauthorizedAccessException: The path specified a file that is read-only. OR this operation is not supported on the current platform. OR the path specified a directory. OR the caller does not have the required permission.OR the mode is Created and the specified file is a hidden file.
  • ArgumentOutOfRangeException: The mode 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 opened in the specified mode and path, with read/write access and not shared.



Below are the programs to illustrate the File.Open(String, FileMode) method.

Program 1: Below code creates a temporary file, writes some specified contents into it, then open the file and print it.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

GFG is a CS Portal.

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

file.txt

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the usage
// of File.Open(String, FileMode) method
  
// Using System, System.IO and
// System.Text namespaces
using System;
using System.IO;
using System.Text;
  
class GFG {
    public static void Main()
    {
        // Specifing a file path
        string path = @"file.txt";
  
        // Opening above file and reading it back.
        using(FileStream fs = File.Open(path, FileMode.Open))
        {
            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));
            }
        }
    }
}

chevron_right


Output:

GeeksforGeeks



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.