prasentation

File opration in c#

Priyanka Rudrawar
3 min readFeb 24, 2022

C# has a wide array of file operations. These operations include opening a file, reading or writing to a file. There can be instances wherein you want to work with files directly, in which case you would use the file operations available in C#. Some of the basic file operations are mentioned below.

  1. Reading — This operation is the basic read operation wherein data is read from a file.
  2. Writing — This operation is the basic write operation wherein data is written to a file. By default, all existing contents are removed from the file, and new content is written.
  3. Appending — This operation also involves writing information to a file. The only difference is that the existing data in a file is not overwritten. The new data to be written is added at the end of the file.

In this prasentation, we will learn-

File.Exists

The File exists method is used to check if a particular file exists. So now let’s see the code which can be used to check if our Example.txt file exists or not. Enter the below code in the program.cs file.

File.ReadAlllines

The method is used to read all the lines one by one in a file. The lines are then stored in a string array variable. Let’s look at an example. Enter the below code in the program.cs file.

File.ReadAllText

This method is used to read all the lines in a file at once. The lines are then stored in a string variable. Let’s look at an example. Enter the below code in the program.cs file.

File.Copy

The method is used to make a copy of an existing file. Let’s look at an example. Enter the below code in the program.cs file.

File.Delete

The method is used to delete an existing file. Let’s look at an example. Enter the below code in the program.cs file.

--

--