DirectoryInfo Class

Definition

Exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class cannot be inherited.

[Serializable]
[ComVisible(true)]
public sealed class DirectoryInfo : FileSystemInfo, ISerializable
Inheritance
DirectoryInfo

Inherited Members

System.IO.FileSystemInfo

System.Object

Constructors

DirectoryInfo(String)

Initializes a new instance of the DirectoryInfo class on the specified path.

public DirectoryInfo(string path)
Parameters
path
String

A string specifying the path on which to create the DirectoryInfo.

Exceptions

path is null.

The caller does not have the required permission.

path contains invalid characters such as ", <, >, or |.

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. The specified path, file name, or both are too long.

Properties

Exists

Gets a value indicating whether the directory exists.

public override bool Exists { get; }
Value
Boolean

true if the directory exists; otherwise, false.

Overrides

Name

Gets the name of this DirectoryInfo instance.

public override string Name { get; }
Value
String

The directory name.

Overrides
See Also

Parent

Gets the parent directory of a specified subdirectory.

public DirectoryInfo Parent { get; }
Value
DirectoryInfo

The parent directory, or null if the path is null or if the file path denotes a root (such as "\", "C:", or * "\server\share").

Exceptions

The caller does not have the required permission.

Root

Gets the root portion of the directory.

public DirectoryInfo Root { get; }
Value
DirectoryInfo

An object that represents the root of the directory.

Exceptions

The caller does not have the required permission.

Methods

Create()

Creates a directory.

public void Create()
Exceptions

The directory cannot be created.

CreateSubdirectory(String)

Creates a subdirectory or subdirectories on the specified path. The specified path can be relative to this instance of the DirectoryInfo class.

public DirectoryInfo CreateSubdirectory(string path)
Parameters
path
String

The specified path. This cannot be a different disk volume or Universal Naming Convention (UNC) name.

Returns

The last directory specified in path.

Exceptions

path does not specify a valid file path or contains invalid DirectoryInfo characters.

path is null.

The specified path is invalid, such as being on an unmapped drive.

The subdirectory cannot be created.

-or-

A file or directory already has the name specified by path.

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. The specified path, file name, or both are too long.

The caller does not have code access permission to create the directory.

-or-

The caller does not have code access permission to read the directory described by the returned DirectoryInfo object. This can occur when the path parameter describes an existing directory.

path contains a colon character (:) that is not part of a drive label ("C:\").

Delete()

Deletes this DirectoryInfo if it is empty.

public override void Delete()
Exceptions

The directory contains a read-only file.

The directory described by this DirectoryInfo object does not exist or could not be found.

The directory is not empty.

-or-

The directory is the application's current working directory.

-or-

There is an open handle on the directory, and the operating system is Windows XP or earlier. This open handle can result from enumerating directories. For more information, see How to: Enumerate Directories and Files.

The caller does not have the required permission.

Overrides

Delete(Boolean)

Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.

public void Delete(bool recursive)
Parameters
recursive
Boolean

true to delete this directory, its subdirectories, and all files; otherwise, false.

Exceptions

The directory contains a read-only file.

The directory described by this DirectoryInfo object does not exist or could not be found.

The directory is read-only.

-or-

The directory contains one or more files or subdirectories and recursive is false.

-or-

The directory is the application's current working directory.

-or-

There is an open handle on the directory or on one of its files, and the operating system is Windows XP or earlier. This open handle can result from enumerating directories and files. For more information, see How to: Enumerate Directories and Files.

The caller does not have the required permission.

EnumerateDirectories()

Returns an enumerable collection of directory information in the current directory.

public IEnumerable<DirectoryInfo> EnumerateDirectories()
Returns

An enumerable collection of directories in the current directory.

Exceptions

The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

EnumerateDirectories(String)

Returns an enumerable collection of directory information that matches a specified search pattern.

public IEnumerable<DirectoryInfo> EnumerateDirectories(string searchPattern)
Parameters
searchPattern
String

The search string to match against the names of directories. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.

Returns

An enumerable collection of directories that matches searchPattern.

Exceptions

searchPattern is null.

The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

EnumerateDirectories(String, SearchOption)

Returns an enumerable collection of directory information that matches a specified search pattern and search subdirectory option.

public IEnumerable<DirectoryInfo> EnumerateDirectories(string searchPattern, SearchOption searchOption)
Parameters
searchPattern
String

The search string to match against the names of directories. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.

searchOption
SearchOption

One of the enumeration values that specifies whether the search operation should include only the current directory or all subdirectories. The default value is TopDirectoryOnly.

Returns

An enumerable collection of directories that matches searchPattern and searchOption.

Exceptions

searchPattern is null.

searchOption is not a valid SearchOption value.

The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

EnumerateFiles()

Returns an enumerable collection of file information in the current directory.

public IEnumerable<FileInfo> EnumerateFiles()
Returns

An enumerable collection of the files in the current directory.

Exceptions

The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

EnumerateFiles(String)

Returns an enumerable collection of file information that matches a search pattern.

public IEnumerable<FileInfo> EnumerateFiles(string searchPattern)
Parameters
searchPattern
String

The search string to match against the names of files. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.

Returns

An enumerable collection of files that matches searchPattern.

Exceptions

searchPattern is null.

The path encapsulated in the DirectoryInfo object is invalid, (for example, it is on an unmapped drive).

The caller does not have the required permission.

EnumerateFiles(String, SearchOption)

Returns an enumerable collection of file information that matches a specified search pattern and search subdirectory option.

public IEnumerable<FileInfo> EnumerateFiles(string searchPattern, SearchOption searchOption)
Parameters
searchPattern
String

The search string to match against the names of files. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.

searchOption
SearchOption

One of the enumeration values that specifies whether the search operation should include only the current directory or all subdirectories. The default value is TopDirectoryOnly.

Returns

An enumerable collection of files that matches searchPattern and searchOption.

Exceptions

searchPattern is null.

searchOption is not a valid SearchOption value.

The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

EnumerateFileSystemInfos()

Returns an enumerable collection of file system information in the current directory.

public IEnumerable<FileSystemInfo> EnumerateFileSystemInfos()
Returns

An enumerable collection of file system information in the current directory.

Exceptions

The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

EnumerateFileSystemInfos(String)

Returns an enumerable collection of file system information that matches a specified search pattern.

public IEnumerable<FileSystemInfo> EnumerateFileSystemInfos(string searchPattern)
Parameters
searchPattern
String

The search string to match against the names of directories. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.

Returns

An enumerable collection of file system information objects that matches searchPattern.

Exceptions

searchPattern is null.

The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

EnumerateFileSystemInfos(String, SearchOption)

Returns an enumerable collection of file system information that matches a specified search pattern and search subdirectory option.

public IEnumerable<FileSystemInfo> EnumerateFileSystemInfos(string searchPattern, SearchOption searchOption)
Parameters
searchPattern
String

The search string to match against the names of directories. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.

searchOption
SearchOption

One of the enumeration values that specifies whether the search operation should include only the current directory or all subdirectories. The default value is TopDirectoryOnly.

Returns

An enumerable collection of file system information objects that matches searchPattern and searchOption.

Exceptions

searchPattern is null.

searchOption is not a valid SearchOption value.

The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

GetDirectories()

Returns the subdirectories of the current directory.

public DirectoryInfo[] GetDirectories()
Returns

An array of DirectoryInfo objects.

Exceptions

The path encapsulated in the DirectoryInfo object is invalid, such as being on an unmapped drive.

The caller does not have the required permission.

The caller does not have the required permission.

GetDirectories(String)

Returns an array of directories in the current DirectoryInfo matching the given search criteria.

public DirectoryInfo[] GetDirectories(string searchPattern)
Parameters
searchPattern
String

The search string to match against the names of directories. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.

Returns

An array of type DirectoryInfo matching searchPattern.

Exceptions

searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPattern is null.

The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

GetDirectories(String, SearchOption)

Returns an array of directories in the current DirectoryInfo matching the given search criteria and using a value to determine whether to search subdirectories.

public DirectoryInfo[] GetDirectories(string searchPattern, SearchOption searchOption)
Parameters
searchPattern
String

The search string to match against the names of directories. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.

searchOption
SearchOption

One of the enumeration values that specifies whether the search operation should include only the current directory or all subdirectories.

Returns

An array of type DirectoryInfo matching searchPattern.

Exceptions

searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPattern is null.

searchOption is not a valid SearchOption value.

The path encapsulated in the DirectoryInfo object is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

GetFiles()

Returns a file list from the current directory.

public FileInfo[] GetFiles()
Returns

An array of type FileInfo.

Exceptions

The path is invalid, such as being on an unmapped drive.

GetFiles(String)

Returns a file list from the current directory matching the given search pattern.

public FileInfo[] GetFiles(string searchPattern)
Parameters
searchPattern
String

The search string to match against the names of files. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.

Returns

An array of type FileInfo.

Exceptions

searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPattern is null.

The path is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

GetFiles(String, SearchOption)

Returns a file list from the current directory matching the given search pattern and using a value to determine whether to search subdirectories.

public FileInfo[] GetFiles(string searchPattern, SearchOption searchOption)
Parameters
searchPattern
String

The search string to match against the names of files. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.

searchOption
SearchOption

One of the enumeration values that specifies whether the search operation should include only the current directory or all subdirectories.

Returns

An array of type FileInfo.

Exceptions

searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPattern is null.

searchOption is not a valid SearchOption value.

The path is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

GetFileSystemInfos()

Returns an array of strongly typed FileSystemInfo entries representing all the files and subdirectories in a directory.

public FileSystemInfo[] GetFileSystemInfos()
Returns

An array of strongly typed FileSystemInfo entries.

Exceptions

The path is invalid (for example, it is on an unmapped drive).

See Also

GetFileSystemInfos(String)

Retrieves an array of strongly typed FileSystemInfo objects representing the files and subdirectories that match the specified search criteria.

public FileSystemInfo[] GetFileSystemInfos(string searchPattern)
Parameters
searchPattern
String

The search string to match against the names of directories and files. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.

Returns

An array of strongly typed FileSystemInfo objects matching the search criteria.

Exceptions

searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPattern is null.

The specified path is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

See Also

GetFileSystemInfos(String, SearchOption)

Retrieves an array of FileSystemInfo objects that represent the files and subdirectories matching the specified search criteria.

public FileSystemInfo[] GetFileSystemInfos(string searchPattern, SearchOption searchOption)
Parameters
searchPattern
String

The search string to match against the names of directories and filesa. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.

searchOption
SearchOption

One of the enumeration values that specifies whether the search operation should include only the current directory or all subdirectories. The default value is TopDirectoryOnly.

Returns

An array of file system entries that match the search criteria.

Exceptions

searchPattern contains one or more invalid characters defined by the GetInvalidPathChars() method.

searchPattern is null.

searchOption is not a valid SearchOption value.

The specified path is invalid (for example, it is on an unmapped drive).

The caller does not have the required permission.

MoveTo(String)

Moves a DirectoryInfo instance and its contents to a new path.

public void MoveTo(string destDirName)
Parameters
destDirName
String

The name and path to which to move this directory. The destination cannot be another disk volume or a directory with the identical name. It can be an existing directory to which you want to add this directory as a subdirectory.

Exceptions

destDirName is null.

destDirName is an empty string (''").

An attempt was made to move a directory to a different volume.

-or-

destDirName already exists.

-or-

You are not authorized to access this path.

-or-

The directory being moved and the destination directory have the same name.

The caller does not have the required permission.

The destination directory cannot be found.

ToString()

Returns the original path that was passed by the user.

public override string ToString()
Returns

Returns the original path that was passed by the user.

Overrides

See Also