Learn About File Handling With C#

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.

  1. using System;  
  2. using System.IO;  
  3. class Test {  
  4.     public static void Main() {  
  5.         using(StreamReader sr = new StreamReader("test.txt")) {  
  6.             String line = sr.ReadToEnd();  
  7.             Console.WriteLine(line);  
  8.             Console.ReadLine();  
  9.         }  
  10.     }  
  11. }  

The output of the code is shown below:

Output


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

  1. using System;  
  2. using System.IO;  
  3. using System.Text;  
  4.   
  5. class Program  
  6. {  
  7.     public static void Main()  
  8.     {  
  9.         string fileName = "test.txt";             
  10.             using (StreamWriter fileStr = File.CreateText(fileName));   
  11.             {  
  12.                 fileStr.WriteLine("the test is writed");  
  13.                  Console.WriteLine("The text is write in my file\n");  
  14.                  Console.ReadLine();  
  15.             }                              
  16.         }  
  17.     }  
Output

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

  1. using System;  
  2. using System.IO;  
  3. using System.Text;  
  4.   
  5. class Program  
  6. {  
  7.     public static void Main()  
  8.     {  
  9.         string fileName = "test.txt";             
  10.             using (StreamWriter fileStr = File.AppendText(fileName))  
  11.             {  
  12.                 fileStr.WriteLine("the test is append");  
  13.                  Console.WriteLine("The text is write in my file\n");  
  14.                  Console.ReadLine();  
  15.             }                              
  16.     }}  

After executing the code the text is appended to the file

Output

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


Recommended Free Ebook
Similar Articles