The first two articles introduce the File class and FileInfo class, which are adequate for File operations. However, we will also add FileStream-related operations, such as the content of StreamReader and StreamWriter. This article describes how to use the Directory class. Let's take a look at what operations the Directory class provides for us.
The Directory class provides a large number of static methods for operating directories. Let's take a look at them one by one.
1. Create a directory
// Abstract: create all directories and subdirectories according to the specified path. Public static DirectoryInfo CreateDirectory (string path); // Abstract: create all directories in the specified path and apply the specified Windows security. Public static DirectoryInfo CreateDirectory (string path, DirectorySecurity directorySecurity );
When using the CreateDirectory method, you can create a level-1 directory instead of creating a level-1 directory.
2. delete a directory
// Abstract: delete an empty directory from a specified path. Public static void Delete (string path); // Abstract: Delete a specified directory and, if instructed, Delete any subdirectories in the directory. Public static void Delete (string path, bool recursive );
We can see through the method annotations that the first method can only delete one empty directory. If you delete a non-empty directory, an error is prompted,
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/163S31001-0.png "alt =" "data-mce-src =" http://www.bkjia.com/uploads/allimg/131228/163S31001-0.png "data-mce-style =" display: block; margin-left: auto; margin-right: auto; "style =" border: 0px; cursor: default; display: block; margin-left: auto; margin-right: auto; "/>
The second method can be used to control whether to recursively Delete sub-directories by specifying the second parameter. Therefore, we recommend that you use the first method when using it. When an exception is caught, then, we recommend that you use the second method to recursively delete it. This is only recommended by the individual, not the fixed mode ).
3. Obtain subdirectories and files
// Obtain the directory set
// Abstract: returns an enumerative set of directory names in a specified path. Public static IEnumerable <string> EnumerateDirectories (string path); // Abstract: returns an enumerative set of directory names that match the search mode in the specified path. Public static IEnumerable <string> EnumerateDirectories (string path, string searchPattern); // Abstract: returns an enumerative set of directory names that match the search mode in the specified path, and searches for subdirectories. Public static IEnumerable <string> EnumerateDirectories (string path, string searchPattern, SearchOption searchOption); // Abstract: returns an enumerative set of file names in a specified path. Public static IEnumerable <string> EnumerateFiles (string path); // Abstract: returns an enumerative set of file names that match the search mode in the specified path. Public static IEnumerable <string> EnumerateFiles (string path, string searchPattern); // Abstract: returns an enumerative set of file names that match the search mode in the specified path, and searches for subdirectories. Public static IEnumerable <string> EnumerateFiles (string path, string searchPattern, SearchOption searchOption); // Abstract: returns an enumerative set of File System items in the specified path. Public static IEnumerable <string> EnumerateFileSystemEntries (string path); // Abstract: returns an enumerative set of File System items in the specified path that match the search mode. Public static IEnumerable <string> EnumerateFileSystemEntries (string path, string searchPattern); // Abstract: returns an enumerative set of file names and directory names that match the search mode in the specified path, you can also search for subdirectories. Public static IEnumerable <string> EnumerateFileSystemEntries (string path, string searchPattern, SearchOption searchOption); // get the directory array // Abstract: get the name of the specified sub-directory. Public static string [] GetDirectories (string path); // Abstract: obtains an array of directories that match the specified search mode from the current directory. Public static string [] GetDirectories (string path, string searchPattern); // Abstract: obtains the array of directories that match the specified search mode in the current directory and uses a value to determine whether to search in the subdirectory. Public static string [] GetDirectories (string path, string searchPattern, SearchOption searchOption );
// Get file // Summary: returns the name of the file in the specified directory. Public static string [] GetFiles (string path); // Abstract: returns the name of the file in the specified directory that matches the specified search mode. Public static string [] GetFiles (string path, string searchPattern); // Abstract: returns the name of the file in the specified directory, this directory matches the specified search mode and uses a value to determine whether to search in the subdirectory. Public static string [] GetFiles (string path, string searchPattern, SearchOption searchOption); // Abstract: returns the names of all files and subdirectories in the specified directory. Public static string [] GetFileSystemEntries (string path); // Abstract: returns an array of File System items that match the specified search criteria. Public static string [] GetFileSystemEntries (string path, string searchPattern); // Abstract: obtains an array of all file names and directory names that match the search mode in the specified path, you can also search for subdirectories. Public static string [] GetFileSystemEntries (string path, string searchPattern, SearchOption searchOption );
Through the above method, we can complete the directory traversal function, so when you practice this section, we hope that the ultimate goal is to complete a small example of directory traversal, in the next article, I will upload a directory traversal program for your reference, hoping to help you.
4. Get and Set Directory Properties
// Abstract: gets a System. Security. AccessControl. DirectorySecurity object that encapsulates the access control list (ACL) item of the specified directory. Public static DirectorySecurity GetAccessControl (string path); // Abstract: gets a System. Security. AccessControl. DirectorySecurity object that encapsulates the specified type of access control list (ACL) items in the specified directory. Public static DirectorySecurity GetAccessControl (string path, AccessControlSections includeSections); // Abstract: Get the Directory creation date and time. Public static DateTime GetCreationTime (string path); // Abstract: gets the date and time of Directory creation. The format is Coordinated Universal Time (UTC ). Public static DateTime GetCreationTimeUtc (string path); // Abstract: returns the date and time of the last access to the specified file or directory. Public static DateTime GetLastAccessTime (string path); // Abstract: returns the date and time of the last access to the specified file or directory. The format is Coordinated Universal Time (UTC ). Public static DateTime GetLastAccessTimeUtc (string path); // Abstract: returns the date and time when the specified file or directory was last written. Public static DateTime GetLastWriteTime (string path); // Abstract: returns the date and time of the last write to the specified file or directory. The format is Coordinated Universal Time (UTC ). Public static DateTime GetLastWriteTimeUtc (string path); // Abstract: Apply the access control list (ACL) item described by System. Security. AccessControl. DirectorySecurity object to the specified directory. Public static void SetAccessControl (string path, DirectorySecurity directorySecurity); // Abstract: Set the creation date and time for the specified file or directory. Public static void SetCreationTime (string path, DateTime creationTime); // Abstract: Set the creation date and time of the specified file or directory. The format is Coordinated Universal Time (UTC ). Public static void SetCreationTimeUtc (string path, DateTime creationTimeUtc); // Abstract: set the date and time of the last access to the specified file or directory. Public static void SetLastAccessTime (string path, DateTime lastAccessTime); // Abstract: sets the date and time of the last access to the specified file or directory. The format is Coordinated Universal Time (UTC ). Public static void SetLastAccessTimeUtc (string path, DateTime lastAccessTimeUtc); // Abstract: set the date and time of the last written directory. Public static void SetLastWriteTime (string path, DateTime lastWriteTime); // Abstract: set the date and time of the last write to a directory. The format is Coordinated Universal Time (UTC ). Public static void SetLastWriteTimeUtc (string path, DateTime lastWriteTimeUtc );
5. Move and determine whether or not there exists
// Abstract: Move the file or directory and its content to a new location. Public static void Move (string sourceDirName, string destDirName); // Abstract: determines whether the specified path references an existing directory on the disk. Public static bool Exists (string path );
6. Obtain the volume information, root information, and current working directory.
// Abstract: Obtain the current working directory of the application. Public static string GetCurrentDirectory (); // Abstract: The volume information, root information, or both of the specified paths are returned. Public static string GetDirectoryRoot (string path); // Abstract: retrieves the name of the logical drive in the format of "<drive letter >:\" on this computer. Public static string [] GetLogicalDrives (); // Abstract: retrieves the parent directory of the specified path, including the absolute path and relative path. Public static DirectoryInfo GetParent (string path); // Abstract: set the current working directory of the application to the specified directory. Public static void SetCurrentDirectory (string path );
Summary
The above is the available methods provided for us in the Directory class. Like the File class, these methods are static. By combining these methods, we can complete all operations on the Directory, this article does not provide examples. In the next article, we will provide a simple directory retrieval tool that uses the above methods.
------------------------------------------------------------------------
Me: 277241073@qq.com
This article from the "chase your dream" blog, please be sure to keep this source http://fengzhongshuang.blog.51cto.com/3565709/1112916