Introduction
In this article, we are going to learn about file handling with C#. In my last article, we saw how to compile a C# program using command and prompt in Windows.
File Handling
To save the file information permanently on the disk or to read the information from the disk through C# is known as File Handling. In C#, the System.IO header file is used to both read and write operations in the file on data streams and files. The namespaces also contains a type that performs compression and decompression on files that enable communication through pipes and ports.
Common terms in File Handling:
File
It provides a static method for creating, copying and more operations that help to create a File stream object.
Directory
It provides a static method for creating, copying and enumeration through directories and subdirectories.
Path
The file that is to be opened.
Mode
The file mode tells what type of operation we want to do with the file, such as read-write. Modes are listed below
Append
If the file doesn’t exist it creates a new file.
Create
It’s used to create a file
Create New
It creates a new file and if it already exists it throws an IO exception
Open
Open an existing file.
Truncate
Open an existing file and cut all stored data.
System.IO
This header file is used for finished file operations. The system.IO namespace contains types that allow reading and writing basic file operations.
The following code is used to create, open, and read the contents of the file.
- using System;
- using System.IO;
- class Test {
- public static void Main() {
- using(StreamReader sr = new StreamReader("test.txt")) {
- String line = sr.ReadToEnd();
- Console.WriteLine(line);
- Console.ReadLine();
- }
- }
- }
The output of the code is shown below:
What is Stream Reader?
It’s used to initialize a new instance of the Stream Reader class for the specified stream. It’s used to read the characters from the file.
The next code is used to write the data in the text file
- using System;
- using System.IO;
- using System.Text;
-
- class Program
- {
- public static void Main()
- {
- string fileName = "test.txt";
- using (StreamWriter fileStr = File.CreateText(fileName));
- {
- fileStr.WriteLine("the test is writed");
- Console.WriteLine("The text is write in my file\n");
- Console.ReadLine();
- }
- }
- }
As the code is executed the content is writren in the file and the contents of the file before the code executes become erased.
Append
We use this code to append the text
- using System;
- using System.IO;
- using System.Text;
-
- class Program
- {
- public static void Main()
- {
- string fileName = "test.txt";
- using (StreamWriter fileStr = File.AppendText(fileName))
- {
- fileStr.WriteLine("the test is append");
- Console.WriteLine("The text is write in my file\n");
- Console.ReadLine();
- }
- }}
After executing the code the text is appended to the file
Conclusion
In my next article, we will some advanced topics in file Handling in C#.
I hope that this article gives some knowledge about file handling in C#.