File handling and directories are essential concepts in any programming language, and C# is no exception. In this guide, we will delve into the various functionalities provided by C# to work with files and directories.
File handling refers to the process of reading from and writing to files, while directories are used to organize and manage files within a file system.
Let’s start with file handling in C#.
Reading from a File:
To read data from a file, you can use the StreamReader class. The following code snippet demonstrates how to read from a file:
“`csharp
using System;
using System.IO;
class Program
{
static void Main()
{
using (StreamReader sr = new StreamReader(“file.txt”))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
}
“`
In this example, we are using the StreamReader class to read the contents of a file named “file.txt”. We create an instance of the StreamReader class using the “using” statement, which ensures that the resources are disposed of properly after use. We then use the ReadLine method to read each line of the file until the end.
Writing to a File:
To write data to a file, you can use the StreamWriter class. The following code snippet demonstrates how to write to a file:
“`csharp
using System;
using System.IO;
class Program
{
static void Main()
{
using (StreamWriter sw = new StreamWriter(“file.txt”))
{
sw.WriteLine(“Hello, World!”);
}
}
}
“`
In this example, we are using the StreamWriter class to write the string “Hello, World!” to a file named “file.txt”. Similar to reading from a file, we create an instance of the StreamWriter class using the “using” statement, which ensures that the resources are disposed of properly after use. We then use the WriteLine method to write the string to the file.
Working with Directories:
C# provides the Directory class to work with directories. You can create, delete, move, and get information about directories using this class. Here are some examples:
Creating a Directory:
“`csharp
using System;
using System.IO;
class Program
{
static void Main()
{
Directory.CreateDirectory(“mydirectory”);
}
}
“`
In this example, we are creating a directory named “mydirectory” using the CreateDirectory method of the Directory class.
Deleting a Directory:
“`csharp
using System;
using System.IO;
class Program
{
static void Main()
{
Directory.Delete(“mydirectory”);
}
}
“`
In this example, we are deleting the directory named “mydirectory” using the Delete method of the Directory class. Note that the directory must be empty for the Delete method to succeed.
Moving a Directory:
“`csharp
using System;
using System.IO;
class Program
{
static void Main()
{
Directory.Move(“mydirectory”, “newdirectory”);
}
}
“`
In this example, we are moving the directory named “mydirectory” to a new directory named “newdirectory” using the Move method of the Directory class.
Getting Information about a Directory:
“`csharp
using System;
using System.IO;
class Program
{
static void Main()
{
DirectoryInfo directoryInfo = new DirectoryInfo(“mydirectory”);
Console.WriteLine($”Directory Name: {directoryInfo.Name}”);
Console.WriteLine($”Parent Directory: {directoryInfo.Parent}”);
Console.WriteLine($”Creation Time: {directoryInfo.CreationTime}”);
}
}
“`
In this example, we are creating an instance of the DirectoryInfo class using the “mydirectory” path. We then use various properties of the DirectoryInfo class, such as Name, Parent, and CreationTime, to get information about the directory.
These are some of the basic file handling and directory operations in C#. You can explore further functionalities provided by the File, StreamReader, StreamWriter, and Directory classes to perform more advanced operations like copying files, accessing file attributes, and traversing directories.