Wednesday, June 17, 2026

C# What is meant by opening a file

Opening a file means creating a file stream, if file exists at the specified path. If you use .NET System.IO.File.Open method, object of FileStream can be created.

A file can be opened to: 

  • create a new file,
  • append to an existing file,
  • overwrite existing file etc.

👉The opening process can throw error in some cases.

The FileStream derives from Stream abstract class. The FileStream provides many useful methods to deal with disk files.

The FileStream object is a very useful resource. This resource must be closed when file operation(read,write or append) is done with the file stream object using File.Close method.

The File.Open method should be done with caution to open a file; if the file is missing at the speciified path, exception is thrown that should be handled by developer, or developer should use File.Exists method to check if the file exists at the specified path. Second, File.Open method in most cases use FileMode to decide whether an existing file will be opened or a new file will be created at the specified file path.

Following Open methods can be used to create an object of FileStream:

  • public static FileStream Open(string path, FileMode mode);
  • public static FileStream Open(string path, FileMode mode, FileAccess access);
  • public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share);
  • public static FileStream Open(string path, FileStreamOptions options);

FileMode enumerations:

  • CreateNew = 1,
  • Create = 2,
  • Open = 3,
  • OpenOrCreate = 4,
  • Truncate = 5,
  • Append = 6

FileAccess enumerations:

  • Read = 1,
  • Write = 2,
  • ReadWrite = 3

Look at the following table to see different File Modes:

File Mode Meaning
CreateNew = 1 Throws error if file exists
Create = 2 Overwrites if file exists
Open = 3 Throws error if file not found
OpenOrCreate = 4 Creates file if file not found
Truncate = 5 Opens an existing file and erase all of its contents immediately (set length to 0).
Append = 6 Opens file if it exists and seeks to the end of the file or creates a new file.

Look at the following table to see different File Access:

File Access Meaning
Read Can Read from a file
Write Can Write into a file
ReadWrite Can Read and Write a file

Example. Create and CreateNew modes
class Demo
{
    static void Main()
    {
        var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string filepath = Path.Combine(documents, "sample.txt");
        File.Open(filepath, FileMode.Create); // overwrites if file exists
        try
        {
            File.Open(filepath, FileMode.CreateNew); // throws error if file exists
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}
Example. Append mode
class Demo
{
    static void Main()
    {
        var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string filepath = Path.Combine(documents, "sample2.txt");
        File.Open(filepath, FileMode.Append); // Creates file if file does not exist
    }
}
Example. Open mode can throw exception
class Demo
{
    static void Main()
    {
        var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string filepath = Path.Combine(documents, "sample3.txt");
        try
        {
            File.Open(filepath, FileMode.Open); // Throws error if file does not exist
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}
Example. Truncate mode can throw exception
class Demo
{
    static void Main()
    {
        var documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string filepath = Path.Combine(documents, "sample3.txt");
        try
        {
            File.Open(filepath, FileMode.Truncate); // Throws error if file does not exist
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

No comments:

Post a Comment

Hot Topics