Working With File Class In C#

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

C# File Class

The File class exposes many static methods for moving, copying, and deleting files. Static methods involve moving a file and 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.

File.Create Method

The File.Create method creates a file in the specified folder.

string path = @ "D:\MyTestFile1.txt";  
FileStream fs = File.Create(path); {  
    Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");  
    fs.Write(info, 0, info.Length);  
    Console.WriteLine("File has been created");  
}

File.Delete Method

The File.Delete method deletes a file with the given name in a specified folder.

static void Main(string[] args) {  
    string path = @ "D:\MyTest1.txt";  
    File.Delete(path);  
    Console.WriteLine("File has been deleted");  
}

File.Open Method

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

Here is a detailed article on File.Open method: Open a File in C# (c-sharpcorner.com)

File.Exists Method

This method determines whether the specified file exists.

static void Main(string[] args) {  
    string path = @ "D:\MyTestFile1.txt";  
    Console.WriteLine(File.Exists(path) ? "File exists." : "File does not exist.");  
}

File.Copy Method

This method copies a file to the specified location.

static void Main(string[] args) {  
    string path = @ "D:\MyTestFile1.txt";  
    string path1 = @ "D:\MyTest1.txt";  
    File.Copy(path, path1);  
    Console.WriteLine("File has been copied");  
}

File.Move Method

This method moves a specified file to a new location. We can specify a different name for the file in the new location.

static void Main(string[] args) {  
    string path = @ "D:\MyTestFile1.txt";  
    string path1 = @ "c:\MyTest1.txt";  
    File.Move(path, path1);  
    Console.WriteLine("File has been moved");  
}

File.OpenRead method

This method opens an existing file for reading.

public static void Main() {  
        string path = @ "d:\MyTest1.txt";  
        if (!File.Exists(path)) {  
            // Create the file.  
            FileStream fs = File.Create(path); {  
                Byte[] info = new UTF8Encoding(true).GetBytes("This is a file");  
                fs.Write(info, 0, info.Length);  
            }  
        }  
        using(FileStream fs = File.OpenRead(path)) {  
            byte[] b = new byte[1024];  
            UTF8Encoding temp = new UTF8Encoding(true);  
            while (fs.Read(b, 0, b.Length) > 0) {  
                Console.WriteLine(temp.GetString(b));  
            }  
        }  

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 important class that provides functionality to deal with files. Learn more about Working with FileInfo Class In C#.

Work with directories and folders: Here is A Complete C# Directory Tutorial


Similar Articles