C#
14 BEST C# Books (2021 Update)
C-SHARP (C#) is a general-purpose, multi-paradigm programming language developed by Microsoft that runs...
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.
In this tutorial, you will learn-
C# and .Net can work with files with the help of several File I/O commands. Let's have a look at some of these commands. For our example, we will assume that we have a file in the D drive called Example.txt.
The file will be a simple text file and have 2 lines as shown below
For our example, we will create a simple Console application and work with our File I/O commands. The console application is the basic one which was created in the earlier tutorial. In the console application, all code is written to the Program.cs file.
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.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Tutorial
{
static void Main(string[] args)
{
String path = @"D:\Example.txt";
if (File.Exists(path))
{
Console.WriteLine("File Exists");
}
Console.ReadKey();
}
}
}Code Explanation:-
When the above code is set, and the project is executed using Visual Studio, you will get the below output.
Output:-
From the above output, you can see that the File.Exists command was executed successfully, and the correct message was displayed in the console window.
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.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Tutorial
{
static void Main(string[] args)
{
String path = @"D:\Example.txt";
String[] lines;
lines = File.ReadAllLines(path);
Console.WriteLine(lines[0]);
Console.WriteLine(lines[1]);
Console.ReadKey();
}
}
}Code Explanation:-
When the above code is set, and the project is run using Visual Studio, you will get the below output.
Output:-
From the output, you can see that the File.ReadAllLines command returned both the lines from our file Example.txt
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.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Tutorial
{
static void Main(string[] args)
{
String path = @"D:\Example.txt";
String lines;
lines = File.ReadAllText(path);
Console.WriteLine(lines);
Console.ReadKey();
}
}
}Code Explanation:-
When the above code is set, and the project is run using Visual Studio, you will get the below output.
Output:-
From the output, you can see that the File.ReadAlltext command returned both the lines from our file Example.txt
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.
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Tutorial
{
static void Main(string[] args)
{
String path = @"D:\Example.txt";
String copypath = @"D:\ExampleNew.txt";
File.Copy(path,copypath);
Console.ReadKey();
}
}
}Code Explanation:-
When the above code is set, and the project is run using Visual Studio, the file Example.txt will be copied to ExampleNew.txt.
The method is used to delete an existing file. Let's look at an example. Enter the below code in the program.cs file.
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DemoApplication
{
class Tutorial
{
static void Main(string[] args)
{
String path = @"D:\Example.txt";
File.Delete(path);
Console.ReadKey();
}
}
}Code Explanation:-
When the above code is set, and the project is run using Visual Studio, the file Example.txt will be deleted from the D drive.
Summary
| File Method | Description |
|---|---|
| File.Exists | File exists method is used to check if a particular file exists. |
| File.ReadAlllines | The method is used to read all the lines one by one in a file. |
| File.ReadAllText | This method is used to read all the lines in a file at once. |
| File.Copy | The method is used to make a copy of an existing file. |
| File.Delete | The method is used to delete an existing file. |
C-SHARP (C#) is a general-purpose, multi-paradigm programming language developed by Microsoft that runs...
Flow Control and conditional statements Flow control and conditional statements are available in...
So far we have seen how to work with C# to create console based applications. But in a real-life...
C# Enumeration An enumeration is used in any programming language to define a constant set of...
C# is one of the languages provided by Microsoft to work with .Net. This language encompasses a...
In our previous tutorial, we have learned about how we can use arrays in C#. Let's have a quick...