Files and Directories

Working with Directories

The .NET Framework also provides a standard API for working with directories (folders)

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

The static Directory type exists to provide methods for working with directories, this includes methods for creating, copying, and deleting directories, as well as discovering files within those directories.

 

Creating a directory

You can easily create a directory using the Directory.CreateDirectory method

public static DirectoryInfo CreateDirectory(string path);
public static DirectoryInfo CreateDirectory(string path, DirectorySecurity security);

This method call returns an instance of DirectoryInfo which is a contextual class scoped to the directory path. TheDirectoryInfo type flows calls to the Directory type, but it allows us to encapsulate a specific target directory as the context for this calls.

To create a directory, you should pass the path to the method:

Directory.CreateDirectory(@".\folder");

If the folder already exists, then there is no work to do. If the folder does exist, it will attept to create it. This includes creating any parent folders that form part of the parent path:

Directory.CreateDirectory(@".\path\to\folder");

 

Deleting a directory

Creating a directory is provided by the Directory.Delete method. This allows you to remove a directory that already exists:

public static void Delete(string path);
public static void Delete(string path, bool recursive);

If the directory you're trying to remove is not empty, it will throw an exception. If you want to delete all files and folders within that directory, you must use the second overload which accepts the boolean `recursive' - this will delete all files and folders under the target folder you are trying to remove.

Directory.Delete(@".\folder", true);

 

Enumerating files in a directory

You can easily discover files within a directory using the Directory.GetFiles method:

public static string[] GetFiles(string path);
public static string[] GetFiles(string path, string searchPattern);
public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption);

These overloads return the full path to each file in the directory. Let's look at the parameters:

  • path - The full path to the folder containing the files
  • searchPattern - A wildcard expression used to match a filename, e.g. *.exe
  • searchOption - An option that allows you to limit your search to the top directory only, or all child directories.

Example:

string[] files = Directory.GetFiles(@".\folder", "*", SearchOption.TopDirectoryOnly);

The above call, with find all files (*) in the .\folder only, whereas:

string[] files = Directory.GetFiles(@".\folder", "*.jpg", SearchOption.AllDirectories);

That call will discover all JPEG images files in all directories under (and including) the folder.

You can then easily enumerate over the files using a loop:

for (int i = 0; i < files.Length; i++)
{
    Console.WriteLine(files[i]);   
}

 

Page 6 of 20