You are on page 1of 3

Assignment of Visual Programming

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.";

// Create the file and write the text to it


File.WriteAllText(filePath, 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";

// Create the directory


Directory.CreateDirectory(directoryPath);
3. FileStream Class: The FileStream class provides a stream for reading and writing files,
allowing you to read and write byte data. You can use the FileStream class to read or
write binary data to a file. For example, the following code creates a new FileStream
object and writes some binary data to a file:

using System.IO;

string filePath = @"C:\myFile.bin";

byte[] data = { 0x01, 0x02, 0x03, 0x04, 0x05 };

// Create a new file stream and write the data to it

using (FileStream stream = new


FileStream(filePath, FileMode.Create))

stream.Write(data, 0, data.Length);

4. StreamReader and StreamWriter Classes: The StreamReader and StreamWriter classes


provide a higher-level interface for reading and writing text files. You can use the
StreamReader and StreamWriter classes to read or write text data to a file. For example,
the following code creates a new StreamWriter object and writes some text data to a file:

using System.IO;
string filePath = @"C:\myFile.txt";
string text = "This is some sample text.";

// Create a new file stream and write the text to it


using StreamWriter
using (StreamWriter writer = new
StreamWriter(filePath))
{
writer.WriteLine(text);
}
To use file handling in C#, you need to first include the System.IO namespace in your program
using the using statement. Then, you can use the various classes and methods provided by the
namespace to perform file handling operations. The using statement ensures that the resources
used by the file handling operations are properly disposed of when they are no longer needed.
Overall, file handling is a crucial part of programming in C#, and understanding how to work
with files is essential for building useful applications that can store and retrieve data from files.

You might also like