This document provides a quick overview of the System.IO.Directory class.
Overview
The System.IO.Directory class is a fundamental class in .NET that provides access to the directory structure of a file system. It allows you to create, read, write, and manipulate files and directories within a specified area of the file system.
It's commonly used for tasks like: reading directory contents, creating new files, and managing file hierarchy structures.
You can use it to easily interact with file system operations through a convenient and predictable API.
Key Methods
Here are some essential methods:
- `GetDirectoryContents(type)`: Returns a directory object.
- `GetFiles(type)`: Returns a list of files.
- `GetDirectories(type)`: Returns a list of directory objects.
- `CreateDirectory(path)`: Creates a new directory at the given path.
- `DeleteDirectory(path)`: Deletes the directory specified by the path.
- `CopyDirectory(sourcePath, destinationPath)`: Copies the contents of the source directory to the destination directory.
Example: Creating a Directory
Let's create a directory called 'my_directory' and then add a file inside it.
First, we'll call `CreateDirectory('my_directory')`.
Then, we'll use `GetDirectoryContents(type)` to get the directory object.
Finally, we'll add a file with a name and content to the directory.
Example: Reading a Directory
Let's get the contents of the directory.
Using `GetDirectoryContents(type)` again, we can retrieve the directory's contents.
Then, we can read and print each file in the directory.