Professional Documents
Culture Documents
File Handling:
File handling in C# is the process of working with files on a computer system. This involves reading,
writing, creating, deleting, and manipulating files that are stored on the file system. To perform these
operations, C# provides a set of classes in the System.IO namespace.
Here are some commonly used classes for file handling in C#:
1. File Class: The File class provides static methods to create, delete, copy, move, and open files.
You can use the File class to create new files, delete existing files, or modify the content of
existing files. For example, the following code creates a new file called "myFile.txt" and writes
some text to it:
using System.IO;
string filePath = @"C:\myFile.txt";
string text = "This is some sample text.";
2. Directory Class: The Directory class provides static methods to create, delete, move, and
enumerate directories and subdirectories. You can use the Directory class to create new
directories, delete existing directories, or navigate through the directory hierarchy. For
example, the following code creates a new directory called "myFolder" in the C:\ drive:
using System.IO;
string directoryPath = @"C:\myFolder";
using System.IO;
stream.Write(data, 0, data.Length);
using System.IO;
string filePath = @"C:\myFile.txt";
string text = "This is some sample text.";