public sealed class Directory
Object
Directory
mscorlib
BCL
Provides information and performs operations on directories.
Implementations are required to preserve the case of file and directory path strings, and to be case sensitive if and only if the current platform is case-sensitive.[Note: In most
Directory
methods that accept path arguments, the path can refer to a file or a directory. ]
System.IO Namespace
Directory Methods
Directory.Delete(System.String, bool) Method
Directory.Delete(System.String) Method
Directory.Exists Method
Directory.GetCreationTime Method
Directory.GetCurrentDirectory Method
Directory.GetDirectories(System.String) Method
Directory.GetDirectories(System.String, System.String) Method
Directory.GetDirectoryRoot Method
Directory.GetFileSystemEntries(System.String) Method
Directory.GetFileSystemEntries(System.String, System.String) Method
Directory.GetFiles(System.String, System.String) Method
Directory.GetFiles(System.String) Method
Directory.GetLastAccessTime Method
Directory.GetLastWriteTime Method
Directory.Move Method
Directory.SetCreationTime Method
Directory.SetCurrentDirectory Method
Directory.SetLastAccessTime Method
Directory.SetLastWriteTime Method
public static void Delete(string path, bool recursive);
Deletes the specified directory and, if indicated, any subdirectories in the directory.
- path
- A String containing the name of the directory to delete. This directory must be writable and cannot contain files unless recursive is true.
- recursive
- Specify
true
to delete subdirectories and files in path; otherwise, specifyfalse
.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.DirectoryNotFoundException The specified path was not found. IOException The directory specified by path is read-only, or recursive is false
and path is not an empty directory.PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.Directory Class, System.IO Namespace
public static void Delete(string path);
Deletes the empty directory specified in path.
- path
- A String containing the name of the directory to delete. This directory must be writable and empty.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.DirectoryNotFoundException The specified path was not found. IOException The directory specified by path is read-only or is not empty. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method behaves identically to System.IO.Directory.Delete(System.String)(path,false
).The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.Directory Class, System.IO Namespace
public static bool Exists(string path);
Returns a Boolean indicating whether the specified directory exists.
- path
- A String containing the name of the directory to check.
true
if the caller has the required permissions and path contains the name of an existing directory; otherwise,false
. If path isnull
, a zero-length string, or contains the name of a file, returnsfalse
.
If the caller does not have sufficient permissions to read the files in the directory specified by path, no exception is thrown and the method returnsfalse
regardless of the existence of path.The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.Directory Class, System.IO Namespace
public static DateTime GetCreationTime(string path);
Returns the creation date and time of the specified file or directory.
- path
- A String containing the name of the file or directory for which to obtain creation date and time information.
A DateTime structure set to the creation date and time for the specified directory. This value is expressed in local time.Platforms that do not support this feature return System.DateTime.MinValue.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.IOException The directory specified by path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method is equivalent to System.IO.File.GetCreationTime(System.String) (path).The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.Directory Class, System.IO Namespace
public static string GetCurrentDirectory();
Returns the application's current working directory.
A String containing the path of the current working directory.Platforms that do not support this feature return System.String.Empty.
Exception Type Condition UnauthorizedAccessException The caller does not have the required permission.
System.IO.Directory Class, System.IO Namespace
public static string[] GetDirectories(string path);
Returns the names of subdirectories in the specified directory.
- path
- A String containing the name of the directory for which an array of subdirectory names is returned.
A String array containing the names of subdirectories in path.
Exception Type Condition ArgumentNullException path is null
.ArgumentException path is a zero-length string, contains only white space, or contains implementation-specific invalid characters. DirectoryNotFoundException path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. IOException path is a file name. UnauthorizedAccessException The caller does not have the required permission.
This method is identical to System.IO.Directory.GetDirectories(System.String) (path , "*").The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
The names returned by this method are prefixed with the directory information provided in path.
System.IO.Directory Class, System.IO Namespace
public static string[] GetDirectories(string path, string searchPattern);
Returns the names of subdirectories in the specified directory that match the specified search pattern.
- path
- A String containing the starting location for the search.
- searchPattern
- A String containing the text pattern to match against the names of subdirectories of path. searchPattern cannot end with "..", or contain ".." followed by System.IO.Path.DirectorySeparatorChar or System.IO.Path.AltDirectorySeparatorChar.
AString
array containing the names of subdirectories of path that match searchPattern.
Exception Type Condition ArgumentNullException path or searchPattern is null
.ArgumentException path is a zero-length string, contains only white space, or contains implementation-specific invalid characters. searchPattern does not contain a valid pattern.
DirectoryNotFoundException path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. IOException path is a file name. UnauthorizedAccessException The caller does not have permission to access the requested information.
The following wild card specifiers are permitted in searchPattern:
The period (".") character, if immediately followed by a wild card specifier, indicates that the period or the empty string matches the pattern. For example, "foo.*" and "foo.?" match "foo". Note that "foo.*" and "foo*" behave identically. If the period is not immediately followed by a wildcard, it has no special meaning (it represents a period).
Wild card Description * Zero or more characters. ? Exactly one character. Characters other than the wild card specifiers represent themselves, for example, the searchPattern string "*t" searches for all names inpath ending with the letter "t". The searchPattern string "s*" searches for all names in path beginning with the letter "s".
The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.Directory Class, System.IO Namespace
public static string GetDirectoryRoot(string path);
Returns the path root component of the specified path.
- path
- A String containing the name of a file or directory.
A String containing the root information for the specified path.Platforms that do not support this feature return System.String.Empty.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullException path is null
.PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method obtains the full path information for path, as returned by System.IO.Path.GetFullPath(System.String) (path) and returns the path root component. The specified path is not required to exist.The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory .]
The following example demonstrates the System.IO.Directory.GetDirectoryRoot(System.String) method.
using System; using System.IO; class GetDirectoryTest { public static void Main() { string [] paths = { @"\ecmatest\examples\pathtests.txt", "pathtests.xyzzy", @"\", @"C:\", @"\\myserver\myshare\foo\bar\baz.txt" }; foreach (string pathString in paths) { string s = Directory.GetDirectoryRoot(pathString); Console.WriteLine("Path: {0} Directory Root is {1}",pathString, s== null? "null":s); } } }The output is
Path: \ecmatest\examples\pathtests.txt Directory Root is C:\
Path: pathtests.xyzzy Directory Root is C:\
Path: \ Directory Root is C:\
Path: C:\ Directory Root is C:\
Path: \\myserver\myshare\foo\bar\baz.txt Directory Root is \\myserver\myshare
System.IO.Directory Class, System.IO Namespace
public static string[] GetFileSystemEntries(string path);
Returns the names of all files and subdirectories in the specified directory.
- path
- A String containing the name of the directory for which file and subdirectory names are returned.
A String array containing the names of file system entries in the specified directory.
Exception Type Condition ArgumentNullException path is null
.ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. DirectoryNotFoundException path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. IOException path is a file name. UnauthorizedAccessException The caller does not have the required permission.
This method is identical to System.IO.Directory.GetFileSystemEntries(System.String) (path, "*").The names returned by this method are prefixed with the directory information provided in path. The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory .]
System.IO.Directory Class, System.IO Namespace
public static string[] GetFileSystemEntries(string path, string searchPattern);
Returns an array of file and directory names matching the specified search criteria.
- path
- A String containing the name of the directory to search.
- searchPattern
- A String containing the text pattern for which to search. searchPattern cannot end with "..", or contain ".." followed by System.IO.Path.DirectorySeparatorChar or System.IO.Path.AltDirectorySeparatorChar .
AString
array containing file and directory names matching the specified search criteria.
Exception Type Condition ArgumentNullException searchPattern or path is null
.ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. -or-
searchPattern does not contain a valid pattern.
DirectoryNotFoundException path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. IOException path is a file name. UnauthorizedAccessException The caller does not have the required permission.
The following wild card specifiers are permitted in searchPattern:
The period (".") character, if immediately followed by a wild card specifier, indicates that the period or the empty string matches the pattern. For example, "foo.*" and "foo.?" match "foo". Note that "foo.*" and "foo*" behave identically. If the period is not immediately followed by a wildcard, it has no special meaning (it represents a period).
Wild card Description * Zero or more characters. ? Exactly one character. Characters other than the wild card specifiers represent themselves, for example, the searchPattern string "*t" searches for all names in path ending with the letter "t". The searchPattern string "s*" searches for all names in path beginning with the letter "s".
The names returned by this method are prefixed with the directory information provided in path. The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.Directory Class, System.IO Namespace
public static string[] GetFiles(string path, string searchPattern);
Returns the names of files in the specified directory that match the specified search pattern.
- path
- A String containing the name of the directory to search.
- searchPattern
- A String containing the text pattern to match against the names of files in path. searchPattern cannot end with "..", or contain ".." followed by System.IO.Path.DirectorySeparatorChar or System.IO.Path.AltDirectorySeparatorChar.
A String array containing the names of files in the specified directory that match the specified search pattern.
Exception Type Condition ArgumentNullException searchPattern or path is null
.ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. -or-
searchPattern does not contain a valid pattern.
IOException path is an existing file name. DirectoryNotFoundException path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
The following wild card specifiers are permitted in searchPattern:
The period (".") character, if immediately followed by a wild card specifier, indicates that the period or the empty string matches the pattern. For example, "foo.*" and "foo.?" match "foo". Note that "foo.*" and "foo*" behave identically. If the period is not immediately followed by a wildcard, it has no special meaning (it represents a period).
Wild card Description * Zero or more characters. ? Exactly one character. Characters other than the wild card specifiers and the period always represent themselves, for example, the searchPattern string "*t" searches for all names in path ending with the letter "t". The searchPattern string "s*" searches for all names in path beginning with the letter "s".
The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.Directory Class, System.IO Namespace
public static string[] GetFiles(string path);
Returns the names of all files in the specified directory.
- path
- A String containing the name of the directory for which file names are returned.
A String array containing the names of the files in the specified directory.Platforms that do not support this feature return
null
.
Exception Type Condition ArgumentNullException path is null.
ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
DirectoryNotFoundException path was not found. IOException path is a file name. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method is identical to System.IO.Directory.GetFiles(System.String) (path, "*").The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.Directory Class, System.IO Namespace
public static DateTime GetLastAccessTime(string path);
Returns the date and time the specified file or directory was last accessed.
- path
- A String containing the name of the file or directory for which to obtain access date and time information.
A DateTime structure set to the date and time the specified file or directory was last accessed. This value is expressed in local time.Platforms that do not support this feature return System.DateTime.MinValue.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.IOException The specified path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method is equivalent to System.IO.File.GetLastAccessTime(System.String) (path).The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.Directory Class, System.IO Namespace
public static DateTime GetLastWriteTime(string path);
Returns the date and time the specified file or directory was last written to.
- path
- A String containing the name of the file or directory for which to obtain modification date and time information.
A DateTime structure set to the date and time the specified file or directory was last written to. This value is expressed in local time.Platforms that do not support this feature return System.DateTime.MinValue.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.IOException The specified path was not found. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
This method is equivalent to System.IO.File.GetLastWriteTime(System.String) (path ).The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.Directory Class, System.IO Namespace
public static void Move(string sourceDirName, string destDirName);
Moves a file or a directory and its contents to a new location.
- sourceDirName
- A String containing the name of the file or directory to move.
- destDirName
- A String containing the new location for sourceDirName. This string cannot identify an existing file or directory.
Exception Type Condition ArgumentException sourceDirName or destDirName is a zero-length string, contains only white space, or contains implementation-specific invalid characters. ArgumentNullException sourceDirName or destDirName is null
.UnauthorizedAccessException The caller does not have the required permission. IOException An attempt was made to move a directory to a different volume, -or-
destDirName already exists.
-or-
sourceDirName and destDirName refer to the same file or directory.
DirectoryNotFoundException sourceDirName was not found. PathTooLongException The length or absolute path information for sourceDirName or destDirName exceeds the system-defined maximum length.
The destDirName argument cannot specify a location on a different disk or volume than sourceDirName. The sourceDirName and destDirName arguments cannot identify the same file or directory.[Note: This method throws a IOException if, for example, you try to move "\mydir" to "\public", and "\public" already exists. You must specify "\public\mydir" as the destDirName.]
The sourceDirName and destDirName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
System.IO.Directory Class, System.IO Namespace
public static void SetCreationTime(string path, DateTime creationTime);
Sets the creation date and time for the specified file or directory.
- path
- A String containing the name of the file or directory for which to set the creation date and time information.
- creationTime
- A DateTime containing the value to set for the creation date and time of path. This value is expressed in local time.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentOutOfRangeException creationTime specifies a value outside the range of date/times permitted for this operation. ArgumentNullException path is null
.FileNotFoundException path was not found. IOException An I/O error occurred while performing the operation. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
On platforms that do not support this feature, this method has no effect. If this feature is supported, the range of dates that is valid for this operation is implementation-specific.
System.IO.Directory Class, System.IO Namespace
public static void SetCurrentDirectory(string path);
Sets the application's current working directory to the specified directory.
- path
- A String containing the path to which the current working directory is set.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.FileNotFoundException path was not found. IOException An I/O error occurred while performing the operation. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. SecurityException The caller does not have the required permission to access unmanaged code.
When the application terminates, the working directory is restored to its original location (the directory where the process was started).The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
On platforms that do not support this feature, this method has no effect.
System.IO.Directory Class, System.IO Namespace
public static void SetLastAccessTime(string path, DateTime lastAccessTime);
Sets the date and time the specified file or directory was last accessed.
- path
- A String containing the name of the file or directory for which to set the access date and time information.
- lastAccessTime
- A DateTime containing the value to set for the access date and time of path. This value is expressed in local time.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.ArgumentOutOfRangeException lastAccessTime specifies a value outside the range of date/times permitted for this operation. FileNotFoundException path was not found. IOException An I/O error occurred while performing the operation. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
The path argument is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
On platforms that do not support this feature, this method has no effect. If this feature is supported, the range of dates that is valid for this operation is implementation-specific.
System.IO.Directory Class, System.IO Namespace
public static void SetLastWriteTime(string path, DateTime lastWriteTime);
Sets the date and time a directory was last written to.
- path
- A String containing the name of the directory for which to set the date and time information.
- lastWriteTime
- A DateTime containing the value to set for the last write date and time of path. This value is expressed in local time.
Exception Type Condition ArgumentException path is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters. ArgumentNullException path is null
.ArgumentOutOfRangeException lastWriteTime specifies a value outside the range of date/times permitted for this operation. FileNotFoundException path was not found. IOException An I/O error occurred while performing the operation. PathTooLongException The length of path or the absolute path information for path exceeds the system-defined maximum length. UnauthorizedAccessException The caller does not have the required permission.
Relative path information is interpreted as relative to the current working directory. [Note: To obtain the current working directory, see System.IO.Directory.GetCurrentDirectory.]
On platforms that do not support this feature, this method has no effect. If this feature is supported, the range of dates that is valid for this operation is implementation-specific.
System.IO.Directory Class, System.IO Namespace