12.13 Retrieving Directory Information

 <  Day Day Up  >  

You want to query various attributes for a given directory.


Technique

Whenever you create a new directory, a DirectoryInfo object is returned from the CreateDirectory method. This class contains several properties that allow you to obtain various bits of information about a certain directory. To create a new DirectoryInfo object on an existing directory, pass the existing path name as a string into the DirectoryInfo constructor:

 
 DirectoryInfo dirInfo = new DirectoryInfo( @"C:\" ); 

Once you obtain a DirectoryInfo object, you can access several properties to obtain information similar to the procedure in Section 12.4, "Using the FileInfo Class to Display File Information." In addition to standard time-based properties, such as CreationTime and LastAccessTime , and the Attributes property, you can also retrieve information about directories relative to the DirectoryInfo object. For instance, the Parent property returns a DirectoryInfo object pointing to the parent directory. The Root points to the root folder of the hard drive:

 
 public void ShowDirInfo() {     Console.WriteLine( "Path: {0}", dirInfo.FullName );     Console.WriteLine( "Root: {0}", dirInfo.Root.ToString() );     if( dirInfo.Parent != null ) Console.WriteLine( "Parent Dir: {0}",         dirInfo.Parent.FullName );     else Console.WriteLine( "Parent Dir: None" );     Console.WriteLine( "Attributes: {0}", dirInfo.Attributes.ToString() );     Console.WriteLine( "Creation Time: {0}", dirInfo.CreationTime.ToString() );     Console.WriteLine( "Last Access Time: {0}",         dirInfo.LastAccessTime.ToString() );     Console.WriteLine( "Last Write Time: {0}",         dirInfo.LastWriteTime.ToString() ); } 

A directory naturally holds a collection of files and additional subdirectories. The GetFiles and GetDirectories methods return a collection of FileInfo and DirectoryInfo objects, respectively. Each of these methods contains an overloaded version that lets you specify a wildcard pattern to narrow the search of files or directories to return. For instance, to return a collection of FileInfo objects for all text files ( .txt ) in a directory, you specify the *.txt wildcard pattern:

 
 Console.WriteLine( "Num Files: {0}", dirInfo.GetFiles("*.txt").Length ); foreach( FileInfo file in dirInfo.GetFiles("*.txt") )     Console.WriteLine( "\t{0}", file.Name ); 

Comments

The FileInfo and DirectoryInfo classes both derive from the FileSystemInfo class, which is why you see a lot of similarities in property and method names . Additionally, both use a caching scheme to improve performance. Whenever an instance of one of those objects is created, it retrieves the current information about a file or directory at the time of creation. If you create a DirectoryInfo object, for example, and don't use it until later in your application, the data at that point might be old because the contents or attributes of the directory might have changed. To update a FileInfo or DirectoryInfo object, call the Refresh method.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net