O'Reilly logo

C# in a Nutshell by Peter Drayton, Ted Neward, Ben Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

DirectoryInfo

Synopsis

This class provides the same functionality as Directory, but in a strongly typed, object-oriented manner. An instance of this type represents a single directory. This class extends FileSystemInfo and implements all its methods. In addition, it adds Parent and Root properties to return the parent and root directories respectively. Similarly, it also supplies GetDirectories() and GetFiles(), to retrieve its subdirectories and files, as well as GetFileSystemInfos(), which returns both the files and subdirectories contained by the current directory. MoveTo() allows you to move a directory from one place to another.

Given the similarity between this type and the Directory type, it may not be clear when one should be used in place of the other. The key difference is the Directory class is a collection of static utility functions, whereas a DirectoryInfo object is an actual object, maintaining state and identity in the classic style of all objects. (In fact, the DirectoryInfo methods often map over to use the corresponding Directory methods.)

public sealed class DirectoryInfo : FileSystemInfo {
// Public Constructors
   public method DirectoryInfo(string path);  
// Public Instance Properties
   public override field bool Exists{get; }                      // overrides FileSystemInfo
   public override field string Name{get; }                      // overrides FileSystemInfo
   public field DirectoryInfo Parent{get; } 
   public field DirectoryInfo Root{get; } 
// Public Instance Methods
   public method void Create(); public ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required