![]() This recursive nature causes all folders to be examined, unless an exception is thrown. It then loops through these folders, first displaying the name, then calling itself, passing the folder name, to look one level deeper. When this method is called it uses the basic version of GetDirectories to obtain the folder list for directories immediately within the provided path. In this case we are going to display all folders starting from the root of the C: drive. The Main method calls ShowAllFoldersUnder, passing in the starting path. The code below shows the basic recursive method. In a real application you could build and return a list or array, or use an iterator to return the values as they are required, thus avoiding the delay caused by reading large folder structures ahead of the time they are needed. We'll create it within a console application and show its activity by outputting folder name as they are examined. Let's start with some simple folder recursion code that recreates the flawed functionality of the GetDirectories method. However, we can circumvent this problem by creating our own recursive folder search code. Rather than ignoring folders to which you have restricted access, the method throws an UnauthorizedAccessException. Key amongst these is that some of the folders that you attempt to read could be configured so that the current user may not access them. String folders = "*", SearchOption.AllDirectories) NB: To run this code you need to reference the System.IO namespace so add the directive, " using System.IO " to your code. This returns a list of folder names that can be found in the root of the C: drive. An example of the GetDirectories method is shown below. You can use further methods of the Directory and File classes to work with the folders and their contents. NET framework, includes a method, named GetDirectories, that allows you to read the folders within a given path, returning their names in a string array. ![]() The Directory class, which is a standard type within the. For example, you may wish to populate a TreeView control with the complete list of directories, or you might read the folders in order to compare them with another directory or with a backup copy. It is common to wish to read the entire folder structure of a disk when you are creating software that works with the file system. An alternative is to recursively read the folders. The standard method in the Directory class can cause problems when used in this way. Some applications must read the folder structure beneath an existing folder or for an entire disk or attached device.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |