Linux

C# Open File

“In this article, the different examples for opening a file will be discussed and implemented in the C# programming language by using the Ubuntu 20.04 command line terminal. The use of classes like FileStream and Stream Writer will be helpful while creating, opening, and reading files, as we will see later on in the different examples. These classes are a part of the namespace System.IO, which has different classes related to all input and output functions.”

The File.Open() Method in the C# Program

The File.Open() method is inherent to the File Stream Class and is accessed by creating an object of the class. The syntax for opening a file in the C# programming language is shown below:

File Stream is a part of the system.IO namespace where File Stream is used for file manipulations like reading, writing, and even finding existing files in a directory. To use File Stream, we will first need to create an object of the File Stream class; the object can be given four parameters that will help us access the files. The FileName parameter consists of the title of the file, while the FileMode parameter specifies the mode in which you want to work on the file. The FileAcess parameter defines the state of the access for a file, and the FileShare parameter is used to determine the sharing of files with other FileStreams objects.

Example 01: Using the File Open Method to Open a Pre-Existent File in Ubuntu 20.04

In this instance, we will be opening a file that is pre-existing in a directory. To access the file, we must provide the file’s name and path to the object of the File stream as a parameter and also give the Mode in which we want to access it.

Text Description automatically generated

In this C# program, we will use the file open method and give it some parameters; one of them will be FileMode.Open. After completing the parameters, we will move on to creating a byte array with the size of “1024”. We will generate an object of the UTF8Encoding class. After that, we will create a read method using the while loop; using the length of the array to run the loop, we will be able to read the file until its end.

Text Description automatically generated

Graphical user interface, text, application Description automatically generated

As we can see in the output screen and the file as well that the Text of the file is the same and was accessed through the File open method.

Example 02: Using the Stream Writer and File Stream Class to Create and Open a File in Ubuntu 20.04

Stream Writer is a class that also belongs to the System.IO namespace. It is used to write characters in a stream in the specified encoding.

Graphical user interface, text, application Description automatically generated

In this C# program, we will generate an object of the class StreamWriter and use the Write() method to write our stream of characters in the file specified. After that, we will use the File.Open function of the File Stream class. Then we will create a byte array and create an object of UTF8encoding class to convert the characters in the file. The While loop will run equal to the length of characters in the file and will exit after reading.

A screenshot of a computer Description automatically generated with medium confidence

As we can see in the output that the file is formed, and the data is saved correctly.

Graphical user interface Description automatically generated with low confidence

Example 03: Using the FileStream Class to Make a Temporary File and Launch it in Ubuntu 20.04

We can use FileStream class to create and access a temporary file. This time we will be using FileStream to create two objects for different purposes. The first time we will build an object to create the temporary file and write some data in it will provide all the parameters, including File access and FileShare too, according to our needs.

A screenshot of a computer Description automatically generated with medium confidence

In this program, we will use the GetTempFileName() method to create a path for and name for the file and FileMode.Open top opens the file FileAccess.Write and FileShare. After this, we will create a Byte array with UTF8encoding; we will also use the GetByte method to allow us to enter data in the file. The Write() function will be used to insert the entered string in a file. Write() method will enter data to the length of the string we gave in Byte Array. Then we will state the File.open method and write all its parameters. After this, we will move on to creating a byte array with a size of “1024”. We will build an object of the UTF8Encoding class.

Text Description automatically generated

As the output suggests, we have opened a text file and successfully added some text to it.

Example 04: Opening and Reading a Text File With Multiple Lines Using the File Stream Class in Ubuntu 20.04

In this instance, we will be opening and accessing a text file. The Read() method is used to read any text.file even with multiple lines using the file stream class, which will be used in this example.

Graphical user interface, text, application Description automatically generated

The Read() method will be used to access the file in this C# program. We will create a byte array with a size of “1024” after finishing the parameters of the File.Open() method. We are going to make a UTF8Encoding class object. Then, using the while loop and the length of the array to execute the loop, we will be able to read the file till it finishes, with the loop ending when all multiple lines have been read.

Text Description automatically generated with medium confidence

The output of the program and the file suggests that the file was accessed and read correctly.

Timeline Description automatically generated with low confidence

Example 05: Opening a Read-only Text File Using the File Stream Class in Ubuntu 20.04

In this instance, we will be focusing on how to access a read-only file using the File Stream class. We will use the File.Open() method to open the file and also try to access and edit the file and see how the compiler will react to this command.

Text Description automatically generated

In this C# program, we will first create a file. Now we will build an object from the FileStream class; this object will be used to access and read the file we just created. To access the file, we will call the File.Open() method and fill the necessary parameters. After that, we will create a read method using the while loop; using the length of the array to run the loop, we will be able to read the file until its end. We will be performing exception handling by using the try and catch method to see if we are allowed to write on the file or not.

Text Description automatically generated

As the output suggests, we can see that the system did not allow us to write in the file, and an error is thrown “Stream does not support writing.”

Conclusion

In this article, we have deliberated the method for opening a file in the C# programming language. We used the File Stream and the Stream Writer class present in the System.IO namespace to create and open files in the C# programming language. We also implemented several examples of opening files in different conditions and several types of data in the file by using the Ubuntu 20.04 Command-line terminal.

Source link

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button