MorkaLork Development

Interesting stuff I've picked up over the years...

Input/Output 6: DirectoryInfo

2009-04-16 21:27:25 | 191 views | directoryinfo directory class contstruct dirinfo directery

The Basics



The Directory class provides a convenient way of manipulating directories. The convenience comes at a price, however: the Directory class performs security checks on all of its methods. If you are going to access the same directory multiple times, you can avoid these security checks by using the DirectoryInfo class, which provides methods similar to those in the Directory class, but with all of its methods instance methods. Therefore, you must first construct a DirectoryInfo object by passing a path to its constructor, as in the following code:



DirectoryInfo dirInfo = new DirectoryInfo("C:/data");



Constructing a DirectoryInfo object does not mean creating a directory if it does not yet exist. To create a directory, you need to call the Create method, as shown in the following code:



DirectoryInfo dirInfo = new DirectoryInfo("
C:/XFiles.txt");
dirInfo.Create();



If the directory already exists, the call to the Create method does not throw an exception. If, after constructing a DirectoryInfo object representing a non-existent directory, however, you call other methods that require that the directory exist, a DirectoryNotFoundException will be thrown. For example, the Delete, GetDirectories, GetFile, and MoveTo methods require the DirectoryInfo object to represent a directory that exists.


Source: http://www.ondotnet.com/pub/a/dotnet/2002/08/05/io.html?page=3


Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.