Open In App

File.OpenWrite() Method in C# with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

File.OpenWrite(String) is an inbuilt File class method that is used to open an existing file or creates a new file for writing.

Syntax: 

public static System.IO.FileStream OpenWrite (string path);

Parameter: This function accepts a parameter which is illustrated below: 

  • path: This is the specified text file that is going to be opened for writing.

Exceptions:
 

  • UnauthorizedAccessException: The caller does not have the required permission. OR the path specified a read-only file or directory.
  • 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.
  • NotSupportedException: The path is in an invalid format.

Return Value: Returns an unshared FileStream object on the specified path with Write access.
Below are the programs to illustrate the File.OpenWrite(String) method.
Program 1: Initially, no file is created. Below code, itself creates a file gfg.txt, writes some texts into it, and prints.
 

C#




// C# program to illustrate the usage
// of File.OpenWrite(String) 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 file
        string path = @"gfg.txt";
  
        // Opening the file for writing
        using(FileStream fs = File.OpenWrite(path))
        {
            // Putting below specified contents
            Byte[] info = new UTF8Encoding(true).GetBytes("GFG is a computer science portal.");
            fs.Write(info, 0, info.Length);
        }
  
        // Opening the file for reading
        using(FileStream fs = File.OpenRead(path))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
  
            while (fs.Read(b, 0, b.Length) > 0) {
                // Printing the contents of the file
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}


Output: 
 

GFG is a computer science portal.

The above code gives the above output and creates a file which is shown below:
 

gfg.txt

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

file.txt

The below code will overwrite the file contents with other specified contents then final contents will be printed.

CSharp




// C# program to illustrate the usage
// of File.OpenWrite(String) 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
        string path = @"file.txt";
  
        // Opening the file for writing
        using(FileStream fs = File.OpenWrite(path))
        {
            // Overwriting the above file with below
            // specified contents
            Byte[] info = new UTF8Encoding(true).GetBytes("GFG is a CS portal.");
            fs.Write(info, 0, info.Length);
        }
  
        // Opening the file for reading
        using(FileStream fs = File.OpenRead(path))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
  
            while (fs.Read(b, 0, b.Length) > 0) {
                // Printing the final contents of the file
                Console.WriteLine(temp.GetString(b));
            }
        }
    }
}


Output: 

GFG is a CS portal.


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