Class Directory
Exposes static methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.
public static class Directory
- Inheritance
-
Directory
- Inherited Members
Methods
CreateDirectory(string)
Creates all directories and subdirectories in the specified path unless they already exist.
public static DirectoryInfo CreateDirectory(string path)
Parameters
path
stringThe directory to create.
Returns
- DirectoryInfo
An object that represents the directory at the specified path. This object is returned regardless of whether a directory at the specified path already exists.
Exceptions
- IOException
The directory specified by path is a file.
Delete(string)
Deletes an empty directory from a specified path.
public static void Delete(string path)
Parameters
path
stringThe name of the empty directory to remove. This directory must be writable and empty.
Exceptions
- IOException
The directory specified by path is not empty.
Delete(string, bool)
Deletes the specified directory and, if indicated, any subdirectories and files in the directory.
public static void Delete(string path, bool recursive)
Parameters
path
stringThe name of the directory to remove.
recursive
booltrue to remove directories, subdirectories, and files in path; otherwise, false.
Remarks
The platform or storage file system may not support recursive deletion of directories. The default value for recursive is false. In this case an exception will be thrown.
Exceptions
- IOException
A file with the same name and location specified by path exists. -or- The directory specified by path is read-only, or recursive is false and path is not an empty directory. -or- The directory is the application's current working directory. -or- The directory contains a read-only file. -or- The directory is being used by another process.
Exists(string)
Determines whether the given path refers to an existing directory on disk.
public static bool Exists(string path)
Parameters
path
stringThe path to test.
Returns
- bool
true if
path
refers to an existing directory; false if the directory does not exist or an error occurs when trying to determine if the specified directory exists.
Exceptions
- ArgumentNullException
Path must be defined.
- IOException
Invalid drive or path to the parent folder doesn't exist.
GetCurrentDirectory()
Gets the current working directory of the application.
public static string GetCurrentDirectory()
Returns
GetDirectories(string)
Returns the names of subdirectories (including their paths) in the specified directory.
public static string[] GetDirectories(string path)
Parameters
path
stringThe relative or absolute path to the directory to search. This string is not case-sensitive.
Returns
- string[]
An array of the full names (including paths) of subdirectories in the specified path, or an empty array if no directories are found.
Exceptions
- IOException
Logical drive or a directory under given path does not exist.
GetFiles(string)
Returns the names of files (including their paths) in the specified directory.
public static string[] GetFiles(string path)
Parameters
path
stringThe relative or absolute path to the directory to search. This string is not case-sensitive.
Returns
- string[]
An array of the full names (including paths) for the files in the specified directory, or an empty array if no files are found.
Exceptions
- IOException
Logical drive or a directory under given path does not exist.
Move(string, string)
Moves a file or a directory and its contents to a new location.
public static void Move(string sourceDirName, string destDirName)
Parameters
sourceDirName
stringThe path of the file or directory to move.
destDirName
stringThe path to the new location for
sourceDirName
or its contents. IfsourceDirName
is a file, thendestDirName
must also be a file name.
Exceptions
- IOException
n attempt was made to move a directory to a different volume. -or- destDirName already exists. See the note in the Remarks section. -or- The source directory does not exist. -or- The source or destination directory name is null. -or- The
sourceDirName
anddestDirName
parameters refer to the same file or directory. -or- The directory or a file within it is being used by another process.
SetCurrentDirectory(string)
Sets the application's current working directory to the specified directory.
public static void SetCurrentDirectory(string path)
Parameters
path
stringThe path to which the current working directory is set.
Exceptions
- IOException
An I/O error occurred.