DirectoryInfo Class
Represents a directory on a file system.
Syntax
public sealed class DirectoryInfo : FileSystemInfo
Description
The DirectoryInfo
class provides instance methods for creating, moving, and enumerating through directories and subdirectories.
It is used to interact with directories on a file system and to obtain information about them.
This class is useful when you need to perform operations on a specific directory, such as listing its contents, checking its existence, or retrieving its attributes.
It inherits from FileSystemInfo
, which provides common properties and methods for file system objects like name, creation time, and last write time.
Members
Constructors
-
DirectoryInfo(string path)
public DirectoryInfo(string path)
Initializes a new instance of the
DirectoryInfo
class.// Example usage: DirectoryInfo di = new DirectoryInfo(@"C:\MyDirectory"); if (di.Exists) { Console.WriteLine("Directory exists."); }
-
DirectoryInfo(string path, IDirectoryProvider provider)
public DirectoryInfo(string path, IDirectoryProvider provider)
Initializes a new instance of the
DirectoryInfo
class with a specific directory provider.
Properties
-
Name
public override string Name { get; }
Gets the name of the directory.
-
FullName
public override string FullName { get; }
Gets the fully qualified path of the directory.
-
Parent
public DirectoryInfo Parent { get; }
Gets a parent directory of the current directory.
-
Root
public DirectoryInfo Root { get; }
Gets the root directory of the specified path.
-
Exists
public override bool Exists { get; }
Gets a value indicating whether the directory exists.
Methods
-
Create()
public void Create()
Creates the directory represented by this instance.
// Example usage: DirectoryInfo newDir = new DirectoryInfo(@"C:\NewProject"); if (!newDir.Exists) { newDir.Create(); Console.WriteLine("Directory created successfully."); }
-
CreateSubdirectory(string path)
public DirectoryInfo CreateSubdirectory(string path)
Creates a subdirectory in the current directory, with the specified name, and returns the related DirectoryInfo object.
-
Delete()
public void Delete()
Deletes this directory. If the directory is not empty, an exception is thrown.
-
Delete(bool recursive)
public void Delete(bool recursive)
Deletes this directory. If
recursive
is true, it deletes this directory and all its subdirectories and files. -
GetDirectories()
public DirectoryInfo[] GetDirectories()
Gets an array of DirectoryInfo objects representing the subdirectories in the current directory.
-
GetFiles()
public FileInfo[] GetFiles()
Gets an array of FileInfo objects representing the files in the current directory.
-
MoveTo(string destDirName)
public void MoveTo(string destDirName)
Moves the DirectoryInfo to a specified path.
Remarks
The DirectoryInfo
class is part of the System.IO
namespace, which provides types that allow reading and writing to and from a variety of data sources and storage media.
When working with file system operations, it's important to handle exceptions such as DirectoryNotFoundException
, UnauthorizedAccessException
, and IOException
.