Working With File Class In C#

C# File Class provides static methods for most of the file operations including create a file, copy and move a file, deleting files, and working with FileStream to read and write streams. The File class is defined in the System.IO namespace.
 

File Class


The File class exposes many static methods for moving, copying, and deleting files. There are static methods that involve moving a file, copying and deleting a file. 

Here is a list of the common File class methods.

S.No Method Description
1 Copy This method is used to copy a file to the specified location.
2 Create This method is used to create a file in the specified path.
3 Delete This method is used to Delete a file.
4 Open This method is used to return a filetream object at the specified path.
5 Move Moves a specified file to a new location. we can specify a different name for the file in the new location.
6 Exists Determines whether the specified file exists.
7 OpenRead Opens an existing file for reading.
8 OpenWrite Opens an existing file or creates a new file for writing.

Create Method


This method is used to create a file in the specified folder.
  1. string path = @ "D:\MyTestFile1.txt";  
  2. FileStream fs = File.Create(path); {  
  3.     Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");  
  4.     fs.Write(info, 0, info.Length);  
  5.     Console.WriteLine("File has been created");  
  6. }  

Delete Method

 
This method is used to Delete a file.
  1. static void Main(string[] args) {  
  2.     string path = @ "D:\MyTest1.txt";  
  3.     File.Delete(path);  
  4.     Console.WriteLine("File has been deleted");  
  5. }  

Open Method


This method is used to return a FileStream object at the specified path. 

Exists Method


Determines whether the specified file exists.
  1. static void Main(string[] args) {  
  2.     string path = @ "D:\MyTestFile1.txt";  
  3.     Console.WriteLine(File.Exists(path) ? "File exists." : "File does not exist.");  
  4. }  

Copy Method


This method is used to copy a file to the specified location.
  1. static void Main(string[] args) {  
  2.     string path = @ "D:\MyTestFile1.txt";  
  3.     string path1 = @ "D:\MyTest1.txt";  
  4.     File.Copy(path, path1);  
  5.     Console.WriteLine("File has been copied");  
  6. }  

Move Method


Moves a specified file to a new location. We can specify a different name for the file in the new location.
  1. static void Main(string[] args) {  
  2.     string path = @ "D:\MyTestFile1.txt";  
  3.     string path1 = @ "c:\MyTest1.txt";  
  4.     File.Move(path, path1);  
  5.     Console.WriteLine("File has been moved");  
  6. }  

OpenRead method

 
Opens an existing file for reading.
  1. public static void Main() {  
  2.         string path = @ "d:\MyTest1.txt";  
  3.         if (!File.Exists(path)) {  
  4.             // Create the file.  
  5.             FileStream fs = File.Create(path); {  
  6.                 Byte[] info = new UTF8Encoding(true).GetBytes("This is a file");  
  7.                 fs.Write(info, 0, info.Length);  
  8.             }  
  9.         }  
  10.         using(FileStream fs = File.OpenRead(path)) {  
  11.             byte[] b = new byte[1024];  
  12.             UTF8Encoding temp = new UTF8Encoding(true);  
  13.             while (fs.Read(b, 0, b.Length) > 0) {  
  14.                 Console.WriteLine(temp.GetString(b));  
  15.             }  
  16.         }  

Conclusion


The File class is very useful for working with files. If there is any mistake in this article then please notify me. I expect your valuable comments and feedback about this article.
 
Next > FileInfo is another importat class provides functionality to deal with files. Learn more Working with FileInfo Class In C#.
 
Work with directories and folders: Here is A Complete C# Directory Tutorial