public sealed class Directory
|
In members that accept a path, the path can refer to a file or just a directory. The specified path can also refer to a relative path or a Universal Naming Convention (UNC) path for a server and share name. For example, all of the following are acceptable paths:
By default, full read/write access to new directories is granted to all users.
CreateDirectory | Creates all directories and subdirectories as specified by path. |
Delete | Overloaded:Delete(string path) Deletes an empty directory from a specified path. |
Delete | Overloaded:Delete(string path, bool recursive) Deletes the specified directory and, if indicated, any subdirectories in the directory. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
Exists | Determines whether the given path refers to an existing directory on disk. |
GetCreationTime | Gets the creation date and time of a directory. |
GetCurrentDirectory | Gets the current working directory of the application. |
GetDirectories | Overloaded:GetDirectories(string path) Gets the names of subdirectories in the specified directory. |
GetDirectories | Overloaded:GetDirectories(string path, string searchPattern) Gets an array of directories matching the specified search pattern from the current directory. |
GetDirectoryRoot | Returns the volume information, root information, or both for the specified path. |
GetFiles | Overloaded:GetFiles(string path) Returns the names of files in the specified directory. |
GetFiles | Overloaded:GetFiles(string path, string searchPattern) Returns the names of files in the specified directory that match the specified search pattern. |
GetFileSystemEntries | Overloaded:GetFileSystemEntries(string path) Returns the names of all files and subdirectories in the specified directory. |
GetFileSystemEntries | Overloaded:GetFileSystemEntries(string path, string searchPattern) Returns an array of file system entries matching the specified search criteria. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetLastAccessTime | Returns the date and time the specified file or directory was last accessed. |
GetLastWriteTime | Returns the date and time the specified file or directory was last written to. |
GetLogicalDrives | Retrieves the names of the logical drives on this computer in the form "<drive letter>:\". |
GetParent | Retrieves the parent directory of the specified path, including both absolute and relative paths. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
Move | Moves a file or a directory and its contents to a new location. |
SetCreationTime | Sets the creation date and time for the specified file or directory. |
SetCurrentDirectory | Sets the application's current working directory to the specified directory. |
SetLastAccessTime | Sets the date and time the specified file or directory was last accessed. |
SetLastWriteTime | Sets the date and time a directory was last written to. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public static DirectoryInfo CreateDirectory( |
path
Exception Type | Condition |
---|---|
IOException | The directory specified by path is read-only or is not empty. |
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The specified path was not found. |
NotSupportedException | Creating a directory with only the colon (:) character was attempted. |
Any and all directories specified in path are created, unless they already exist or unless some part of path is invalid. The path parameter specifies a directory path, not a file path.
Creating a directory with only the colon (:) character is not supported, and will cause a NotSupportedException to be thrown.
In Visual Basic:
Directory.CreateDirectory("Public\Html") Directory.CreateDirectory("\Users\User1\Public\Html") Directory.CreateDirectory("c:\Users\User1\Public\Html")
In C# or C++:
Directory::CreateDirectory("Public\\Html"); Directory::CreateDirectory("\\Users\\User1\\Public\\Html"); Directory::CreateDirectory("c:\\Users\\User1\\Public\\Html");
public static void Delete( |
path
Exception Type | Condition |
---|---|
IOException | The directory specified by path is read-only or is not empty. |
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The specified path was not found. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
This method throws an IOException if the directory specified in the path parameter contains files or subdirectories.
The path parameter is not case-sensitive.
path
recursive
Exception Type | Condition |
---|---|
IOException | The directory specified by path is read-only, or recursive is false and path is not an empty directory. |
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The specified path was not found. |
The path parameter is not case-sensitive.
path
The path parameter is not case-sensitive.
// For File.Exists, Directory.Exists using System; using System.IO; using System.Collections; // Takes an array of file names or directory names on the command line. // Determines what kind of name it is and processes it appropriately public class RecursiveFileProcessor { public static void Main(string[] args) { foreach(string path in args) { if(File.Exists(path)) { // This path is a file ProcessFile(path); } else if(Directory.Exists(path)) { // This path is a directory ProcessDirectory(path); } else { Console.WriteLine("{0} is not a valid file or directory.", path); } } } // Process all files in the directory passed in, and recurse on any directories // that are found to process the files they contain public static void ProcessDirectory(string targetDirectory) { // Process the list of files found in the directory string [] fileEntries = Directory.GetFiles(targetDirectory); foreach(string fileName in fileEntries) ProcessFile(fileName); // Recurse into subdirectories of this directory string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory); foreach(string subdirectory in subdirectoryEntries) ProcessDirectory(subdirectory); } // Real logic for processing found files would go here. public static void ProcessFile(string path) { Console.WriteLine("Processed file '{0}'.", path); } }
~Directory(); |
path
Exception Type | Condition |
---|---|
IOException | The directory specified by path is read-only or is not empty. |
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The specified path was not found. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
The path parameter is not case-sensitive.
public static string GetCurrentDirectory(); |
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
path
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The specified path was not found. |
This method is identical to GetDirectories.
The names returned by this method are prefixed with the directory information provided in path.
The path parameter is not case-sensitive.
// For Directory.GetFiles and Directory.GetDirectories // For File.Exists, Directory.Exists using System; using System.IO; using System.Collections; // Takes an array of file names or directory names on the command line. // Determines what kind of name it is and processes it appropriately public class RecursiveFileProcessor { public static void Main(string[] args) { foreach(string path in args) { if(File.Exists(path)) { // This path is a file ProcessFile(path); } else if(Directory.Exists(path)) { // This path is a directory ProcessDirectory(path); } else { Console.WriteLine("{0} is not a valid file or directory.", path); } } } // Process all files in the directory passed in, and recurse on any directories // that are found to process the files they contain public static void ProcessDirectory(string targetDirectory) { // Process the list of files found in the directory string [] fileEntries = Directory.GetFiles(targetDirectory); foreach(string fileName in fileEntries) ProcessFile(fileName); // Recurse into subdirectories of this directory string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory); foreach(string subdirectory in subdirectoryEntries) ProcessDirectory(subdirectory); } // Real logic for processing found files would go here. public static void ProcessFile(string path) { Console.WriteLine("Processed file '{0}'.", path); } }
path
searchPattern
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. -or- searchPattern does not contain a valid pattern. |
ArgumentNullException | path or searchPattern is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The specified path was not found. |
Wildcard character | Description |
---|---|
* | Zero or more characters. |
? | Exactly one character. |
Characters other than the wildcard 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 path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
The path parameter is not case-sensitive.
path
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
The path parameter is not case-sensitive.
path
Exception Type | Condition |
---|---|
IOException | The directory specified by path is read-only or is not empty. |
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The specified path was not found. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
The path parameter is not case-sensitive.
// For Directory.GetFiles and Directory.GetDirectories // For File.Exists, Directory.Exists using System; using System.IO; using System.Collections; // Takes an array of file names or directory names on the command line. // Determines what kind of name it is and processes it appropriately public class RecursiveFileProcessor { public static void Main(string[] args) { foreach(string path in args) { if(File.Exists(path)) { // This path is a file ProcessFile(path); } else if(Directory.Exists(path)) { // This path is a directory ProcessDirectory(path); } else { Console.WriteLine("{0} is not a valid file or directory.", path); } } } // Process all files in the directory passed in, and recurse on any directories // that are found to process the files they contain public static void ProcessDirectory(string targetDirectory) { // Process the list of files found in the directory string [] fileEntries = Directory.GetFiles(targetDirectory); foreach(string fileName in fileEntries) ProcessFile(fileName); // Recurse into subdirectories of this directory string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory); foreach(string subdirectory in subdirectoryEntries) ProcessDirectory(subdirectory); } // Real logic for processing found files would go here. public static void ProcessFile(string path) { Console.WriteLine("Processed file '{0}'.", path); } }
path
searchPattern
Exception Type | Condition |
---|---|
IOException | The directory specified by path is read-only or is not empty. |
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. -or- searchPattern does not contain a valid pattern. |
ArgumentNullException | path or searchPattern is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The specified path was not found. |
Wildcard character | 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 path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
The path paramenter is not case-sensitive.
path
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The specified path was not found. -or- path is a file name. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
The path parameter is not case-sensitive.
using System; namespace GetFileSystemEntries { class Class1 { static void Main(string[] args) { Class1 snippets = new Class1(); string path = System.IO.Directory.GetCurrentDirectory(); string filter = "*.exe"; snippets.PrintFileSystemEntries(path); snippets.PrintFileSystemEntries(path, filter); snippets.GetLogicalDrives(); snippets.GetParent(path); snippets.Move("C:\\proof", "C:\\Temp"); } void PrintFileSystemEntries(string path) { try { // Obtain the file system entries in the directory path. string[] directoryEntries = System.IO.Directory.GetFileSystemEntries(path); foreach (string str in directoryEntries) { System.Console.WriteLine(str); } } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.DirectoryNotFoundException) { System.Console.WriteLine("The path encapsulated in the " + "Directory object does not exist."); } } void PrintFileSystemEntries(string path, string pattern) { try { // Obtain the file system entries in the directory // path that match the pattern. string[] directoryEntries = System.IO.Directory.GetFileSystemEntries(path, pattern); foreach (string str in directoryEntries) { System.Console.WriteLine(str); } } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.DirectoryNotFoundException) { System.Console.WriteLine("The path encapsulated in the " + "Directory object does not exist."); } } // Print out all logical drives on the system. void GetLogicalDrives() { try { string[] drives = System.IO.Directory.GetLogicalDrives(); foreach (string str in drives) { System.Console.WriteLine(str); } } catch (System.IO.IOException) { System.Console.WriteLine("An I/O error occurs."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } } void GetParent(string path) { try { System.IO.DirectoryInfo directoryInfo = System.IO.Directory.GetParent(path); System.Console.WriteLine(directoryInfo.FullName); } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, or " + "contains invalid characters."); } } void Move(string sourcePath, string destinationPath) { try { System.IO.Directory.Move(sourcePath, destinationPath); System.Console.WriteLine("The directory move is complete."); } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.IOException) { System.Console.WriteLine("An attempt was made to move a " + "directory to a different " + "volume, or destDirName " + "already exists."); } } } }
path
searchPattern
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. -or- searchPattern does not contain a valid pattern. |
ArgumentNullException | path or searchPattern is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The specified path was not found. -or- path is a file name. |
Wildcard character | 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 path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
The path parameter is not case-sensitive.
using System; namespace GetFileSystemEntries { class Class1 { static void Main(string[] args) { Class1 snippets = new Class1(); string path = System.IO.Directory.GetCurrentDirectory(); string filter = "*.exe"; snippets.PrintFileSystemEntries(path); snippets.PrintFileSystemEntries(path, filter); snippets.GetLogicalDrives(); snippets.GetParent(path); snippets.Move("C:\\proof", "C:\\Temp"); } void PrintFileSystemEntries(string path) { try { // Obtain the file system entries in the directory path. string[] directoryEntries = System.IO.Directory.GetFileSystemEntries(path); foreach (string str in directoryEntries) { System.Console.WriteLine(str); } } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.DirectoryNotFoundException) { System.Console.WriteLine("The path encapsulated in the " + "Directory object does not exist."); } } void PrintFileSystemEntries(string path, string pattern) { try { // Obtain the file system entries in the directory // path that match the pattern. string[] directoryEntries = System.IO.Directory.GetFileSystemEntries(path, pattern); foreach (string str in directoryEntries) { System.Console.WriteLine(str); } } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.DirectoryNotFoundException) { System.Console.WriteLine("The path encapsulated in the " + "Directory object does not exist."); } } // Print out all logical drives on the system. void GetLogicalDrives() { try { string[] drives = System.IO.Directory.GetLogicalDrives(); foreach (string str in drives) { System.Console.WriteLine(str); } } catch (System.IO.IOException) { System.Console.WriteLine("An I/O error occurs."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } } void GetParent(string path) { try { System.IO.DirectoryInfo directoryInfo = System.IO.Directory.GetParent(path); System.Console.WriteLine(directoryInfo.FullName); } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, or " + "contains invalid characters."); } } void Move(string sourcePath, string destinationPath) { try { System.IO.Directory.Move(sourcePath, destinationPath); System.Console.WriteLine("The directory move is complete."); } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.IOException) { System.Console.WriteLine("An attempt was made to move a " + "directory to a different " + "volume, or destDirName " + "already exists."); } } } }
public virtual int GetHashCode(); |
path
Exception Type | Condition |
---|---|
IOException | The specified path was not found. |
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
GetLastAccessTime throws an IOException if the directory specified by path does not exist.
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
The path parameter is not case-sensitive.
path
Exception Type | Condition |
---|---|
IOException | The specified path was not found. |
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
The path parameter is not case-sensitive.
public static string[] GetLogicalDrives(); |
Exception Type | Condition |
---|---|
IOException | An I/O error occurs (for example, a disk error). |
SecurityException | The caller does not have the required permission. |
using System; namespace GetFileSystemEntries { class Class1 { static void Main(string[] args) { Class1 snippets = new Class1(); string path = System.IO.Directory.GetCurrentDirectory(); string filter = "*.exe"; snippets.PrintFileSystemEntries(path); snippets.PrintFileSystemEntries(path, filter); snippets.GetLogicalDrives(); snippets.GetParent(path); snippets.Move("C:\\proof", "C:\\Temp"); } void PrintFileSystemEntries(string path) { try { // Obtain the file system entries in the directory path. string[] directoryEntries = System.IO.Directory.GetFileSystemEntries(path); foreach (string str in directoryEntries) { System.Console.WriteLine(str); } } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.DirectoryNotFoundException) { System.Console.WriteLine("The path encapsulated in the " + "Directory object does not exist."); } } void PrintFileSystemEntries(string path, string pattern) { try { // Obtain the file system entries in the directory // path that match the pattern. string[] directoryEntries = System.IO.Directory.GetFileSystemEntries(path, pattern); foreach (string str in directoryEntries) { System.Console.WriteLine(str); } } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.DirectoryNotFoundException) { System.Console.WriteLine("The path encapsulated in the " + "Directory object does not exist."); } } // Print out all logical drives on the system. void GetLogicalDrives() { try { string[] drives = System.IO.Directory.GetLogicalDrives(); foreach (string str in drives) { System.Console.WriteLine(str); } } catch (System.IO.IOException) { System.Console.WriteLine("An I/O error occurs."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } } void GetParent(string path) { try { System.IO.DirectoryInfo directoryInfo = System.IO.Directory.GetParent(path); System.Console.WriteLine(directoryInfo.FullName); } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, or " + "contains invalid characters."); } } void Move(string sourcePath, string destinationPath) { try { System.IO.Directory.Move(sourcePath, destinationPath); System.Console.WriteLine("The directory move is complete."); } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.IOException) { System.Console.WriteLine("An attempt was made to move a " + "directory to a different " + "volume, or destDirName " + "already exists."); } } } }
public static DirectoryInfo GetParent( |
path
Exception Type | Condition |
---|---|
IOException | The directory specified by path is read-only. |
SecurityException | The caller does not have the required permission. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
DirectoryNotFoundException | The specified path was not found. |
The path parameter is not case-sensitive.
using System; namespace GetFileSystemEntries { class Class1 { static void Main(string[] args) { Class1 snippets = new Class1(); string path = System.IO.Directory.GetCurrentDirectory(); string filter = "*.exe"; snippets.PrintFileSystemEntries(path); snippets.PrintFileSystemEntries(path, filter); snippets.GetLogicalDrives(); snippets.GetParent(path); snippets.Move("C:\\proof", "C:\\Temp"); } void PrintFileSystemEntries(string path) { try { // Obtain the file system entries in the directory path. string[] directoryEntries = System.IO.Directory.GetFileSystemEntries(path); foreach (string str in directoryEntries) { System.Console.WriteLine(str); } } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.DirectoryNotFoundException) { System.Console.WriteLine("The path encapsulated in the " + "Directory object does not exist."); } } void PrintFileSystemEntries(string path, string pattern) { try { // Obtain the file system entries in the directory // path that match the pattern. string[] directoryEntries = System.IO.Directory.GetFileSystemEntries(path, pattern); foreach (string str in directoryEntries) { System.Console.WriteLine(str); } } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.DirectoryNotFoundException) { System.Console.WriteLine("The path encapsulated in the " + "Directory object does not exist."); } } // Print out all logical drives on the system. void GetLogicalDrives() { try { string[] drives = System.IO.Directory.GetLogicalDrives(); foreach (string str in drives) { System.Console.WriteLine(str); } } catch (System.IO.IOException) { System.Console.WriteLine("An I/O error occurs."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } } void GetParent(string path) { try { System.IO.DirectoryInfo directoryInfo = System.IO.Directory.GetParent(path); System.Console.WriteLine(directoryInfo.FullName); } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, or " + "contains invalid characters."); } } void Move(string sourcePath, string destinationPath) { try { System.IO.Directory.Move(sourcePath, destinationPath); System.Console.WriteLine("The directory move is complete."); } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.IOException) { System.Console.WriteLine("An attempt was made to move a " + "directory to a different " + "volume, or destDirName " + "already exists."); } } } }
public Type GetType(); |
protected object MemberwiseClone(); |
sourceDirName
destDirName
Exception Type | Condition |
---|---|
IOException | An attempt was made to move a directory to a different volume -or- destDirName already exists -or- The sourceDirName and destDirName parameters refer to the same file or directory. |
SecurityException | The caller does not have the required permission. |
ArgumentException | sourceDirName or destDirName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | sourceDirName or destDirName is null. |
PathTooLongException | The length or absolute path information for sourceDirName or destDirName exceeds the system-defined maximum length. |
DirectoryNotFoundException | sourceDirName was not found. |
This method permits moving a directory to a read-only directory. The read/write attribute of neither directory is affected.
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. To obtain the current working directory, see Directory.GetCurrentDirectory.
using System; namespace GetFileSystemEntries { class Class1 { static void Main(string[] args) { Class1 snippets = new Class1(); string path = System.IO.Directory.GetCurrentDirectory(); string filter = "*.exe"; snippets.PrintFileSystemEntries(path); snippets.PrintFileSystemEntries(path, filter); snippets.GetLogicalDrives(); snippets.GetParent(path); snippets.Move("C:\\proof", "C:\\Temp"); } void PrintFileSystemEntries(string path) { try { // Obtain the file system entries in the directory path. string[] directoryEntries = System.IO.Directory.GetFileSystemEntries(path); foreach (string str in directoryEntries) { System.Console.WriteLine(str); } } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.DirectoryNotFoundException) { System.Console.WriteLine("The path encapsulated in the " + "Directory object does not exist."); } } void PrintFileSystemEntries(string path, string pattern) { try { // Obtain the file system entries in the directory // path that match the pattern. string[] directoryEntries = System.IO.Directory.GetFileSystemEntries(path, pattern); foreach (string str in directoryEntries) { System.Console.WriteLine(str); } } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.DirectoryNotFoundException) { System.Console.WriteLine("The path encapsulated in the " + "Directory object does not exist."); } } // Print out all logical drives on the system. void GetLogicalDrives() { try { string[] drives = System.IO.Directory.GetLogicalDrives(); foreach (string str in drives) { System.Console.WriteLine(str); } } catch (System.IO.IOException) { System.Console.WriteLine("An I/O error occurs."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } } void GetParent(string path) { try { System.IO.DirectoryInfo directoryInfo = System.IO.Directory.GetParent(path); System.Console.WriteLine(directoryInfo.FullName); } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, or " + "contains invalid characters."); } } void Move(string sourcePath, string destinationPath) { try { System.IO.Directory.Move(sourcePath, destinationPath); System.Console.WriteLine("The directory move is complete."); } catch (ArgumentNullException) { System.Console.WriteLine("Path is a null reference."); } catch (System.Security.SecurityException) { System.Console.WriteLine("The caller does not have the " + "required permission."); } catch (ArgumentException) { System.Console.WriteLine("Path is an empty string, " + "contains only white spaces, " + "or contains invalid characters."); } catch (System.IO.IOException) { System.Console.WriteLine("An attempt was made to move a " + "directory to a different " + "volume, or destDirName " + "already exists."); } } } }
path
creationTime
Exception Type | Condition |
---|---|
IOException | The specified path was not found. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
ArgumentOutOfRangeException | creationTime specifies a value outside the range of dates or times permitted for this operation. |
The path parameter is not case-sensitive.
public static void SetCurrentDirectory( |
path
Exception Type | Condition |
---|---|
IOException | The directory specified by path is read-only or is not empty. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
PathTooLongException | The length of path or the absolute path information for path exceeds the system-defined maximum length. |
FileNotFoundException | The specified path was not found. |
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.
The path parameter is not case-sensitive.
path
lastAccessTime
Exception Type | Condition |
---|---|
IOException | The specified path was not found. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
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. |
The path parameter is not case-sensitive.
path
lastWriteTime
Exception Type | Condition |
---|---|
IOException | The specified path was not found. |
ArgumentException | path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by Path.InvalidPathChars. |
ArgumentNullException | path is null. |
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. |
The path parameter is not case-sensitive.
public virtual string ToString(); |