Files and Directories

Utility Types

The .NET Framework provides a number of utility types that can be used to assist in I/O operations.

 

using System; using System.IO; public c...

FileInfo and File

When working with files, previously we've used FileStream - but this assumes that you know where the file is and that it actually exists. If you need to work out information about a file, you have two options - the FileInfo and File classes.

The first class, FileInfo is a meta class that uses attributes from the file system, and surfaces that information as properties, such as Exists (for determining whether a file actually exists on disk) and LastWriteTime (a DateTime value representing the date/time the file was last written).

FileInfo file = new FileInfo(@"C:\Windows\Notepad.exe");
if (file.Exists)
{
    Console.WriteLine("The file {0} exists on disk, and was last accessed on {1}", file.Name, file.LastAccessTime);
}

You can also perform operations for copying or moving files:

file.CopyTo(newPath);
file.MoveTo(newPath);

The File type is a static class providing helper methods. Internally this often delegates to FileInfo for the operations, but it does provide a simple API for managing files.

string file = @"C:\Windows\Notepad.exe";
if (File.Exists(file))
{
    Console.WriteLine("The file {0} exists on disk", file);
}

The File type provides useful instance methods for reading and writing files:

string text = File.ReadAllText(path);
string[] lines = File.ReadAllLines(path);

File.WriteAllText(path, text);
File.WriteAllLines(path, lines);

Many of the operations available to the FileInfo type are surfaced on the File static class, including copy and move operations:

File.Copy(fromPath, toPath);
File.Move(fromPath, toPath);

 

DirectoryInfo and Directory

Much like the FileInfo and File types, there exists the DirectoryInfo and Directory types. These types are specialised for working with directories instead of files.

The DirectoryInfo type is a meta class for reading attributes from the file system when working with directories. This includes information about whether the directory exists (Exists) and what child directories are available (through the GetDirectories()) method. It also makes it easy to read the name of the directory (Name) and full path (FullName).

DirectoryInfo directory = new DirectoryInfo(@"C:\Windows");
if (directory.Exists)
{
    Console.WriteLine("The directory {0} exists", directory.Name);
}

To obtain a list of child directories of the same directory:

The Directory type is a static helper class for performing many of the same operations as the DirectoryInfo type.

string directory = @"C:\Windows";
if (Directory.Exists(directory))
{
    Console.WriteLine("The directory {0} exists", directory);
}

To obtain a list of child directories, you can use the GetDirectories method:

foreach (string childDirectory in Directory.GetDirectories(@"C:\Windows"))
{
    Console.WriteLine(childDirectory);
}

The two APIs different in that the DirectoryInfo often returns other DirectoryInfo types (e.g.DirectoryInfo.GetDirectories), whereas the Directory type returns strings.

 

Path

The Path type is a static helper class that provides methods for working with file and directory paths.

string filePath = @"C:\Windows\Notepad.exe";
string fileDirectory = Path.GetDirectoryName(filePath);
string fileName = Path.GetFileName(filePath);

The Path type is useful for understanding the make-up of a path, including how it is rooted (e.g. "C:\" or "\server"), the directory structure, the OS-specific directory separator and the filename with extension information.

You can easily get just the filename from a full path using GetFileName:

string fileName = Path.GetFileName(@"C:\Windows\Notepad.exe");
Console.WriteLine(fileName); // Notepad.exe

If you want to get the directory that contains a file

string directory = Path.GetDirectoryName(@"C:\Windows\Notepad.exe");
Console.WriteLine(directory); // C:\Windows

You can also combine paths together using the OS-specific directory separator:

string path = Path.Combine("C:", "Windows", "Notepad.exe");
Console.WriteLine(path); // C:\Windows\Notepad.exe

 

Page 16 of 20