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 ...

Get C# in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.