Early Preview

This is currently very much a preview. Please feel free to try things out, but don't be upset if anything is not yet working. Feedback is welcome over on our GitHub Dicussions page.

class System.​IO.​Directory

Assembly: System.Runtime

Inheritance: object → Directory

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

Methods

public static DirectoryInfo
CreateDirectory​(string path)
Creates all directories and subdirectories in the specified path unless they already exist.
Returns 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.
path The directory to create.
public static DirectoryInfo
CreateDirectory​(string path, UnixFileMode unixCreateMode)
Creates all directories and subdirectories in the specified path with the specified permissions unless they already exist.
Returns 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.
path The directory to create.
unixCreateMode A bitwise combination of the enumeration values that specifies the Unix file mode used to create directories.
public static FileSystemInfo
CreateSymbolicLink​(string path, string pathToTarget)
Creates a directory symbolic link identified by <paramref name="path" /> that points to <paramref name="pathToTarget" /> .
Returns A <see cref="T:System.IO.DirectoryInfo" /> instance that wraps the newly created directory symbolic link.
path The path where the symbolic link should be created.
pathToTarget The target directory of the symbolic link.
public static DirectoryInfo
CreateTempSubdirectory​(string prefix = null)
Creates a uniquely named, empty directory in the current user's temporary directory.
Returns An object that represents the directory that was created.
prefix An optional string to add to the beginning of the subdirectory name.
public static void
Delete​(string path)
Deletes an empty directory from a specified path.
path The name of the empty directory to remove. This directory must be writable and empty.
public static void
Delete​(string path, bool recursive)
Deletes the specified directory and, if indicated, any subdirectories and files in the directory.
path The name of the directory to remove.
recursive <see langword="true" /> to remove directories, subdirectories, and files in <paramref name="path" /> ; otherwise, <see langword="false" /> .
public static Collections.​Generic.​IEnumerable<​string>
EnumerateDirectories​(string path)
Returns an enumerable collection of directory full names in a specified path.
Returns An enumerable collection of the full names (including paths) for the directories in the directory specified by <paramref name="path" /> .
path The relative or absolute path to the directory to search. This string is not case-sensitive.
public static Collections.​Generic.​IEnumerable<​string>
EnumerateDirectories​(string path, string searchPattern)
Returns an enumerable collection of directory full names that match a search pattern in a specified path.
Returns An enumerable collection of the full names (including paths) for the directories in the directory specified by <paramref name="path" /> and that match the specified search pattern.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of directories in <paramref name="path" /> . This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
public static Collections.​Generic.​IEnumerable<​string>
EnumerateDirectories​(string path, string searchPattern, EnumerationOptions enumerationOptions)
Returns an enumerable collection of the directory full names that match a search pattern in a specified path, and optionally searches subdirectories.
Returns An enumerable collection of the full names (including paths) for the directories in the directory specified by <paramref name="path" /> and that match the specified search pattern and enumeration options.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of directories in <paramref name="path" /> . This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
enumerationOptions An object that describes the search and enumeration configuration to use.
public static Collections.​Generic.​IEnumerable<​string>
EnumerateDirectories​(string path, string searchPattern, SearchOption searchOption)
Returns an enumerable collection of directory full names that match a search pattern in a specified path, and optionally searches subdirectories.
Returns An enumerable collection of the full names (including paths) for the directories in the directory specified by <paramref name="path" /> and that match the specified search pattern and search option.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of directories in <paramref name="path" /> . This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
searchOption One of the enumeration values that specifies whether the search operation should include only the current directory or should include all subdirectories. The default value is <see cref="F:System.IO.SearchOption.TopDirectoryOnly" /> .
public static Collections.​Generic.​IEnumerable<​string>
EnumerateFiles​(string path)
Returns an enumerable collection of full file names in a specified path.
Returns An enumerable collection of the full names (including paths) for the files in the directory specified by <paramref name="path" /> .
path The relative or absolute path to the directory to search. This string is not case-sensitive.
public static Collections.​Generic.​IEnumerable<​string>
EnumerateFiles​(string path, string searchPattern)
Returns an enumerable collection of full file names that match a search pattern in a specified path.
Returns An enumerable collection of the full names (including paths) for the files in the directory specified by <paramref name="path" /> and that match the specified search pattern.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of files in <paramref name="path" /> . This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
public static Collections.​Generic.​IEnumerable<​string>
EnumerateFiles​(string path, string searchPattern, EnumerationOptions enumerationOptions)
Returns an enumerable collection of full file names that match a search pattern and enumeration options in a specified path, and optionally searches subdirectories.
Returns An enumerable collection of the full names (including paths) for the files in the directory specified by <paramref name="path" /> and that match the specified search pattern and enumeration options.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of files in <paramref name="path" /> . This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
enumerationOptions An object that describes the search and enumeration configuration to use.
public static Collections.​Generic.​IEnumerable<​string>
EnumerateFiles​(string path, string searchPattern, SearchOption searchOption)
Returns an enumerable collection of full file names that match a search pattern in a specified path, and optionally searches subdirectories.
Returns An enumerable collection of the full names (including paths) for the files in the directory specified by <paramref name="path" /> and that match the specified search pattern and search option.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of files in <paramref name="path" /> . This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
searchOption One of the enumeration values that specifies whether the search operation should include only the current directory or should include all subdirectories. The default value is <see cref="F:System.IO.SearchOption.TopDirectoryOnly" /> .
public static Collections.​Generic.​IEnumerable<​string>
EnumerateFileSystemEntries​(string path)
Returns an enumerable collection of file names and directory names in a specified path.
Returns An enumerable collection of file-system entries in the directory specified by <paramref name="path" /> .
path The relative or absolute path to the directory to search. This string is not case-sensitive.
public static Collections.​Generic.​IEnumerable<​string>
EnumerateFileSystemEntries​(string path, string searchPattern)
Returns an enumerable collection of file names and directory names that match a search pattern in a specified path.
Returns An enumerable collection of file-system entries in the directory specified by <paramref name="path" /> and that match the specified search pattern.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of file-system entries in <paramref name="path" /> . This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
public static Collections.​Generic.​IEnumerable<​string>
EnumerateFileSystemEntries​(string path, string searchPattern, EnumerationOptions enumerationOptions)
Returns an enumerable collection of file names and directory names that match a search pattern and enumeration options in a specified path.
Returns An enumerable collection of file-system entries in the directory specified by <paramref name="path" /> , that match the specified search pattern and the specified enumeration options.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of files and directories in <paramref name="path" /> . This parameter can contain a combination of valid literal and wildcard characters, but it doesn't support regular expressions.
enumerationOptions An object that describes the search and enumeration configuration to use.
public static Collections.​Generic.​IEnumerable<​string>
EnumerateFileSystemEntries​(string path, string searchPattern, SearchOption searchOption)
Returns an enumerable collection of file names and directory names that match a search pattern in a specified path, and optionally searches subdirectories.
Returns An enumerable collection of file-system entries in the directory specified by <paramref name="path" /> and that match the specified search pattern and option.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against file-system entries in <paramref name="path" /> . This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
searchOption One of the enumeration values that specifies whether the search operation should include only the current directory or should include all subdirectories. The default value is <see cref="F:System.IO.SearchOption.TopDirectoryOnly" /> .
public static bool
Exists​(string path)
Determines whether the given path refers to an existing directory on disk.
Returns <see langword="true" /> if <paramref name="path" /> refers to an existing directory; <see langword="false" /> if the directory does not exist or an error occurs when trying to determine if the specified directory exists.
path The path to test.
public static DateTime
GetCreationTime​(string path)
Gets the creation date and time of a directory.
Returns A structure that is set to the creation date and time for the specified directory. This value is expressed in local time.
path The path of the directory.
public static DateTime
GetCreationTimeUtc​(string path)
Gets the creation date and time, in Coordinated Universal Time (UTC) format, of a directory.
Returns A structure that is set to the creation date and time for the specified directory. This value is expressed in UTC time.
path The path of the directory.
public static string
GetCurrentDirectory​()
Gets the current working directory of the application.
Returns A string that contains the absolute path of the current working directory, and does not end with a backslash (\).
public static string[]
GetDirectories​(string path)
Returns the names of subdirectories (including their paths) in the specified directory.
Returns An array of the full names (including paths) of subdirectories in the specified path, or an empty array if no directories are found.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
public static string[]
GetDirectories​(string path, string searchPattern)
Returns the names of subdirectories (including their paths) that match the specified search pattern in the specified directory.
Returns An array of the full names (including paths) of the subdirectories that match the search pattern in the specified directory, or an empty array if no directories are found.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of subdirectories in <paramref name="path" /> . This parameter can contain a combination of valid literal and wildcard characters, but it doesn't support regular expressions.
public static string[]
GetDirectories​(string path, string searchPattern, EnumerationOptions enumerationOptions)
Returns the names of subdirectories (including their paths) that match the specified search pattern and enumeration options in the specified directory.
Returns An array of the full names (including paths) of the subdirectories that match the search pattern and enumeration options in the specified directory, or an empty array if no directories are found.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of subdirectories in <paramref name="path" /> . This parameter can contain a combination of valid literal and wildcard characters, but it doesn't support regular expressions.
enumerationOptions An object that describes the search and enumeration configuration to use.
public static string[]
GetDirectories​(string path, string searchPattern, SearchOption searchOption)
Returns the names of the subdirectories (including their paths) that match the specified search pattern in the specified directory, and optionally searches subdirectories.
Returns An array of the full names (including paths) of the subdirectories that match the specified criteria, or an empty array if no directories are found.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of subdirectories in <paramref name="path" /> . This parameter can contain a combination of valid literal and wildcard characters, but it doesn't support regular expressions.
searchOption One of the enumeration values that specifies whether the search operation should include all subdirectories or only the current directory.
public static string
GetDirectoryRoot​(string path)
Returns the volume information, root information, or both for the specified path.
Returns A string that contains the volume information, root information, or both for the specified path.
path The path of a file or directory.
public static string[]
GetFiles​(string path)
Returns the names of files (including their paths) in the specified directory.
Returns An array of the full names (including paths) for the files in the specified directory, or an empty array if no files are found.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
public static string[]
GetFiles​(string path, string searchPattern)
Returns the names of files (including their paths) that match the specified search pattern in the specified directory.
Returns An array of the full names (including paths) for the files in the specified directory that match the specified search pattern, or an empty array if no files are found.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of files in <paramref name="path" /> . This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
public static string[]
GetFiles​(string path, string searchPattern, EnumerationOptions enumerationOptions)
Returns the names of files (including their paths) that match the specified search pattern and enumeration options in the specified directory.
Returns An array of the full names (including paths) for the files in the specified directory that match the specified search pattern and enumeration options, or an empty array if no files are found.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of files in <paramref name="path" /> . This parameter can contain a combination of valid literal and wildcard characters, but it doesn't support regular expressions.
enumerationOptions An object that describes the search and enumeration configuration to use.
public static string[]
GetFiles​(string path, string searchPattern, SearchOption searchOption)
Returns the names of files (including their paths) that match the specified search pattern in the specified directory, using a value to determine whether to search subdirectories.
Returns An array of the full names (including paths) for the files in the specified directory that match the specified search pattern and option, or an empty array if no files are found.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of files in <paramref name="path" /> . This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
searchOption One of the enumeration values that specifies whether the search operation should include all subdirectories or only the current directory.
public static string[]
GetFileSystemEntries​(string path)
Returns the names of all files and subdirectories in a specified path.
Returns An array of the names of files and subdirectories in the specified directory, or an empty array if no files or subdirectories are found.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
public static string[]
GetFileSystemEntries​(string path, string searchPattern)
Returns an array of file names and directory names that match a search pattern in a specified path.
Returns An array of file names and directory names that match the specified search criteria, or an empty array if no files or directories are found.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of files and directories in <paramref name="path" /> . This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
public static string[]
GetFileSystemEntries​(string path, string searchPattern, EnumerationOptions enumerationOptions)
Returns an array of file names and directory names that match a search pattern and enumeration options in a specified path.
Returns An array of file names and directory names that match the specified search pattern and enumeration options, or an empty array if no files or directories are found.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of files and directories in <paramref name="path" /> . This parameter can contain a combination of valid literal and wildcard characters, but it doesn't support regular expressions.
enumerationOptions An object that describes the search and enumeration configuration to use.
public static string[]
GetFileSystemEntries​(string path, string searchPattern, SearchOption searchOption)
Returns an array of all the file names and directory names that match a search pattern in a specified path, and optionally searches subdirectories.
Returns An array of file the file names and directory names that match the specified search criteria, or an empty array if no files or directories are found.
path The relative or absolute path to the directory to search. This string is not case-sensitive.
searchPattern The search string to match against the names of files and directories in <paramref name="path" /> . This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
searchOption One of the enumeration values that specifies whether the search operation should include only the current directory or should include all subdirectories. The default value is <see cref="F:System.IO.SearchOption.TopDirectoryOnly" /> .
public static DateTime
GetLastAccessTime​(string path)
Returns the date and time the specified file or directory was last accessed.
Returns A structure that is set to the date and time the specified file or directory was last accessed. This value is expressed in local time.
path The file or directory for which to obtain access date and time information.
public static DateTime
GetLastAccessTimeUtc​(string path)
Returns the date and time, in Coordinated Universal Time (UTC) format, that the specified file or directory was last accessed.
Returns A structure that is set to the date and time the specified file or directory was last accessed. This value is expressed in UTC time.
path The file or directory for which to obtain access date and time information.
public static DateTime
GetLastWriteTime​(string path)
Returns the date and time the specified file or directory was last written to.
Returns A structure that is set to the date and time the specified file or directory was last written to. This value is expressed in local time.
path The file or directory for which to obtain modification date and time information.
public static DateTime
GetLastWriteTimeUtc​(string path)
Returns the date and time, in Coordinated Universal Time (UTC) format, that the specified file or directory was last written to.
Returns A structure that is set to the date and time the specified file or directory was last written to. This value is expressed in UTC time.
path The file or directory for which to obtain modification date and time information.
public static string[]
GetLogicalDrives​()
Retrieves the names of the logical drives on this computer.
Returns An array of strings representing the logical drive names.
public static DirectoryInfo
GetParent​(string path)
Retrieves the parent directory of the specified path, including both absolute and relative paths.
Returns The parent directory, or <see langword="null" /> if <paramref name="path" /> is the root directory, including the root of a UNC server or share name.
path The path for which to retrieve the parent directory.
public static void
Move​(string sourceDirName, string destDirName)
Moves a file or a directory and its contents to a new location.
sourceDirName The path of the file or directory to move.
destDirName The path to the new location for <paramref name="sourceDirName" /> or its contents. If <paramref name="sourceDirName" /> is a file, then <paramref name="destDirName" /> must also be a file name.
public static FileSystemInfo
ResolveLinkTarget​(string linkPath, bool returnFinalTarget)
Gets the target of the specified directory link.
Returns A <see cref="T:System.IO.DirectoryInfo" /> instance if <paramref name="linkPath" /> exists, independently if the target exists or not. <see langword="null" /> if <paramref name="linkPath" /> is not a link.
linkPath The path of the directory link.
returnFinalTarget <see langword="true" /> to follow links to the final target; <see langword="false" /> to return the immediate next link.
public static void
SetCreationTime​(string path, DateTime creationTime)
Sets the creation date and time for the specified file or directory.
path The file or directory for which to set the creation date and time information.
creationTime The date and time the file or directory was last written to. This value is expressed in local time.
public static void
SetCreationTimeUtc​(string path, DateTime creationTimeUtc)
Sets the creation date and time, in Coordinated Universal Time (UTC) format, for the specified file or directory.
path The file or directory for which to set the creation date and time information.
creationTimeUtc The date and time the directory or file was created. This value is expressed in local time.
public static void
SetCurrentDirectory​(string path)
Sets the application's current working directory to the specified directory.
path The path to which the current working directory is set.
public static void
SetLastAccessTime​(string path, DateTime lastAccessTime)
Sets the date and time the specified file or directory was last accessed.
path The file or directory for which to set the access date and time information.
lastAccessTime An object that contains the value to set for the access date and time of <paramref name="path" /> . This value is expressed in local time.
public static void
SetLastAccessTimeUtc​(string path, DateTime lastAccessTimeUtc)
Sets the date and time, in Coordinated Universal Time (UTC) format, that the specified file or directory was last accessed.
path The file or directory for which to set the access date and time information.
lastAccessTimeUtc An object that contains the value to set for the access date and time of <paramref name="path" /> . This value is expressed in UTC time.
public static void
SetLastWriteTime​(string path, DateTime lastWriteTime)
Sets the date and time a directory was last written to.
path The path of the directory.
lastWriteTime The date and time the directory was last written to. This value is expressed in local time.
public static void
SetLastWriteTimeUtc​(string path, DateTime lastWriteTimeUtc)
Sets the date and time, in Coordinated Universal Time (UTC) format, that a directory was last written to.
path The path of the directory.
lastWriteTimeUtc The date and time the directory was last written to. This value is expressed in UTC time.
public bool
Equals​(object obj)
Inherited from object
Determines whether the specified object is equal to the current object.
Returns <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" /> .
obj The object to compare with the current object.
protected void
Finalize​()
Inherited from object
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
public int
GetHashCode​()
Inherited from object
Serves as the default hash function.
Returns A hash code for the current object.
public Type
GetType​()
Inherited from object
Gets the <see cref="T:System.Type" /> of the current instance.
Returns The exact runtime type of the current instance.
protected object
MemberwiseClone​()
Inherited from object
Creates a shallow copy of the current <see cref="T:System.Object" /> .
Returns A shallow copy of the current <see cref="T:System.Object" /> .
public string
ToString​()
Inherited from object
Returns a string that represents the current object.
Returns A string that represents the current object.