System.IO.Directory Class

public sealed class Directory

Base Types

Object
  Directory

Assembly

mscorlib

Library

BCL

Summary

Provides information and performs operations on directories.

Description

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. ]

See Also

System.IO Namespace

Members

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


Directory.Delete(System.String, bool) Method

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

Summary

Deletes the specified directory and, if indicated, any subdirectories in the directory.

Parameters

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, specify false .

Exceptions

Exception TypeCondition
ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullExceptionpath is null .
DirectoryNotFoundExceptionThe specified path was not found.
IOExceptionThe directory specified by path is read-only, or recursive is false and path is not an empty directory.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

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.]

See Also

System.IO.Directory Class, System.IO Namespace

Directory.Delete(System.String) Method

public static void Delete(string path);

Summary

Deletes the empty directory specified in path.

Parameters

path
A String containing the name of the directory to delete. This directory must be writable and empty.

Exceptions

Exception TypeCondition
ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullExceptionpath is null .
DirectoryNotFoundExceptionThe specified path was not found.
IOExceptionThe directory specified by path is read-only or is not empty.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

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.]

See Also

System.IO.Directory Class, System.IO Namespace

Directory.Exists Method

public static bool Exists(string path);

Summary

Returns a Boolean indicating whether the specified directory exists.

Parameters

path
A String containing the name of the directory to check.

Return Value

true if the caller has the required permissions and path contains the name of an existing directory; otherwise, false . If path is null , a zero-length string, or contains the name of a file, returns false .

Description

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 returns false 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.]

See Also

System.IO.Directory Class, System.IO Namespace

Directory.GetCreationTime Method

public static DateTime GetCreationTime(string path);

Summary

Returns the creation date and time of the specified file or directory.

Parameters

path
A String containing the name of the file or directory for which to obtain creation date and time information.

Return Value

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.

Exceptions

Exception TypeCondition
ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullExceptionpath is null .
IOExceptionThe directory specified by path was not found.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

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.]

See Also

System.IO.Directory Class, System.IO Namespace

Directory.GetCurrentDirectory Method

public static string GetCurrentDirectory();

Summary

Returns the application's current working directory.

Return Value

A String containing the path of the current working directory.

Platforms that do not support this feature return System.String.Empty.

Exceptions

Exception TypeCondition
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

See Also

System.IO.Directory Class, System.IO Namespace

Directory.GetDirectories(System.String) Method

public static string[] GetDirectories(string path);

Summary

Returns the names of subdirectories in the specified directory.

Parameters

path
A String containing the name of the directory for which an array of subdirectory names is returned.

Return Value

A String array containing the names of subdirectories in path.

Exceptions

Exception TypeCondition
ArgumentNullExceptionpath is null .
ArgumentExceptionpath is a zero-length string, contains only white space, or contains implementation-specific invalid characters.
DirectoryNotFoundExceptionpath was not found.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
IOExceptionpath is a file name.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

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.

See Also

System.IO.Directory Class, System.IO Namespace

Directory.GetDirectories(System.String, System.String) Method

public static string[] GetDirectories(string path, string searchPattern);

Summary

Returns the names of subdirectories in the specified directory that match the specified search pattern.

Parameters

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.

Return Value

A String array containing the names of subdirectories of path that match searchPattern.

Exceptions

Exception TypeCondition
ArgumentNullExceptionpath or searchPattern is null .
ArgumentExceptionpath is a zero-length string, contains only white space, or contains implementation-specific invalid characters.

searchPattern does not contain a valid pattern.

DirectoryNotFoundExceptionpath was not found.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
IOExceptionpath is a file name.
UnauthorizedAccessExceptionThe caller does not have permission to access the requested information.

Description

The following wild card specifiers are permitted in searchPattern:

Wild cardDescription
*Zero or more characters.
?Exactly one character.
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).

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.]

See Also

System.IO.Directory Class, System.IO Namespace

Directory.GetDirectoryRoot Method

public static string GetDirectoryRoot(string path);

Summary

Returns the path root component of the specified path.

Parameters

path
A String containing the name of a file or directory.

Return Value

A String containing the root information for the specified path.

Platforms that do not support this feature return System.String.Empty.

Exceptions

Exception TypeCondition
ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.

ArgumentNullExceptionpath is null .
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

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 .]

Example

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

See Also

System.IO.Directory Class, System.IO Namespace

Directory.GetFileSystemEntries(System.String) Method

public static string[] GetFileSystemEntries(string path);

Summary

Returns the names of all files and subdirectories in the specified directory.

Parameters

path
A String containing the name of the directory for which file and subdirectory names are returned.

Return Value

A String array containing the names of file system entries in the specified directory.

Exceptions

Exception TypeCondition
ArgumentNullExceptionpath is null .
ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
DirectoryNotFoundExceptionpath was not found.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
IOExceptionpath is a file name.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

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 .]

See Also

System.IO.Directory Class, System.IO Namespace

Directory.GetFileSystemEntries(System.String, System.String) Method

public static string[] GetFileSystemEntries(string path, string searchPattern);

Summary

Returns an array of file and directory names matching the specified search criteria.

Parameters

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 .

Return Value

A String array containing file and directory names matching the specified search criteria.

Exceptions

Exception TypeCondition
ArgumentNullExceptionsearchPattern or path is null .
ArgumentExceptionpath 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.

DirectoryNotFoundExceptionpath was not found.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
IOExceptionpath is a file name.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

The following wild card specifiers are permitted in searchPattern:

Wild cardDescription
* Zero or more characters.
?Exactly one character.
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).

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.]

See Also

System.IO.Directory Class, System.IO Namespace

Directory.GetFiles(System.String, System.String) Method

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

Summary

Returns the names of files in the specified directory that match the specified search pattern.

Parameters

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.

Return Value

A String array containing the names of files in the specified directory that match the specified search pattern.

Exceptions

Exception TypeCondition
ArgumentNullExceptionsearchPattern or path is null .
ArgumentExceptionpath 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.

IOExceptionpath is an existing file name.
DirectoryNotFoundExceptionpath was not found.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

The following wild card specifiers are permitted in searchPattern:

Wild cardDescription
*Zero or more characters.
?Exactly one character.
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).

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.]

See Also

System.IO.Directory Class, System.IO Namespace

Directory.GetFiles(System.String) Method

public static string[] GetFiles(string path);

Summary

Returns the names of all files in the specified directory.

Parameters

path
A String containing the name of the directory for which file names are returned.

Return Value

A String array containing the names of the files in the specified directory.

Platforms that do not support this feature return null .

Exceptions

Exception TypeCondition
ArgumentNullExceptionpath is null.

ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.

DirectoryNotFoundExceptionpath was not found.
IOExceptionpath is a file name.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

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.]

See Also

System.IO.Directory Class, System.IO Namespace

Directory.GetLastAccessTime Method

public static DateTime GetLastAccessTime(string path);

Summary

Returns the date and time the specified file or directory was last accessed.

Parameters

path
A String containing the name of the file or directory for which to obtain access date and time information.

Return Value

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.

Exceptions

Exception TypeCondition
ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullExceptionpath is null .
IOExceptionThe specified path was not found.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

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.]

See Also

System.IO.Directory Class, System.IO Namespace

Directory.GetLastWriteTime Method

public static DateTime GetLastWriteTime(string path);

Summary

Returns the date and time the specified file or directory was last written to.

Parameters

path
A String containing the name of the file or directory for which to obtain modification date and time information.

Return Value

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.

Exceptions

Exception TypeCondition
ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullExceptionpath is null .
IOExceptionThe specified path was not found.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

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.]

See Also

System.IO.Directory Class, System.IO Namespace

Directory.Move Method

public static void Move(string sourceDirName, string destDirName);

Summary

Moves a file or a directory and its contents to a new location.

Parameters

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.

Exceptions

Exception TypeCondition
ArgumentExceptionsourceDirName or destDirName is a zero-length string, contains only white space, or contains implementation-specific invalid characters.
ArgumentNullExceptionsourceDirName or destDirName is null .
UnauthorizedAccessExceptionThe caller does not have the required permission.
IOExceptionAn 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.

DirectoryNotFoundExceptionsourceDirName was not found.
PathTooLongExceptionThe length or absolute path information for sourceDirName or destDirName exceeds the system-defined maximum length.

Description

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.]

See Also

System.IO.Directory Class, System.IO Namespace

Directory.SetCreationTime Method

public static void SetCreationTime(string path, DateTime creationTime);

Summary

Sets the creation date and time for the specified file or directory.

Parameters

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.

Exceptions

Exception TypeCondition
ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentOutOfRangeExceptioncreationTime specifies a value outside the range of date/times permitted for this operation.
ArgumentNullExceptionpath is null .
FileNotFoundExceptionpath was not found.
IOExceptionAn I/O error occurred while performing the operation.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

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.

See Also

System.IO.Directory Class, System.IO Namespace

Directory.SetCurrentDirectory Method

public static void SetCurrentDirectory(string path);

Summary

Sets the application's current working directory to the specified directory.

Parameters

path
A String containing the path to which the current working directory is set.

Exceptions

Exception TypeCondition
ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullExceptionpath is null .
FileNotFoundExceptionpath was not found.
IOExceptionAn I/O error occurred while performing the operation.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
SecurityExceptionThe caller does not have the required permission to access unmanaged code.

Description

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.

See Also

System.IO.Directory Class, System.IO Namespace

Directory.SetLastAccessTime Method

public static void SetLastAccessTime(string path, DateTime lastAccessTime);

Summary

Sets the date and time the specified file or directory was last accessed.

Parameters

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.

Exceptions

Exception TypeCondition
ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullExceptionpath is null .
ArgumentOutOfRangeExceptionlastAccessTime specifies a value outside the range of date/times permitted for this operation.
FileNotFoundExceptionpath was not found.
IOExceptionAn I/O error occurred while performing the operation.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

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.

See Also

System.IO.Directory Class, System.IO Namespace

Directory.SetLastWriteTime Method

public static void SetLastWriteTime(string path, DateTime lastWriteTime);

Summary

Sets the date and time a directory was last written to.

Parameters

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.

Exceptions

Exception TypeCondition
ArgumentExceptionpath is a zero-length string, contains only white space, or contains one or more implementation-specific invalid characters.
ArgumentNullExceptionpath is null .
ArgumentOutOfRangeExceptionlastWriteTime specifies a value outside the range of date/times permitted for this operation.
FileNotFoundExceptionpath was not found.
IOExceptionAn I/O error occurred while performing the operation.
PathTooLongExceptionThe length of path or the absolute path information for path exceeds the system-defined maximum length.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Description

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.

See Also

System.IO.Directory Class, System.IO Namespace