Saturday, 3 February 2007

DirectoryInfo Class

Exposes instance methods for creating, moving, and enumerating through directories and subdirectories. This class is sealed and cannot be inherited.

If you are going to reuse an object several times, consider using the instance method of DirectoryInfo instead of the corresponding static methods of Directory class, because a security check will not always be necessary.

Common Properties
  • Parent (Gets the DirectoryInfo object for the parent directory of the current directory in the directory hierarchy).
  • Root (Gets the root part of the directory's path as a string).

Common Members

  • Create (Creates the directory described in the current DirectoryInfo object).
  • CreateSubdirectory (Creates a new directory as a child directory of the current directory in the directory hierarchy).
  • GetDirectories (Retrieves an array of DirectoryInfo objects that represent subdirectories of the current directory).
  • GetFiles (Retrieves an array of FileInfo objects that represent all the files in the current directory).
  • GetFileSystemInfos (Retrieves an array of FileSystemInfo objects that represent both files and subdirectories in the current directory).
  • MoveTo (Moves the current directory to a new location).

    //
    Specify the directories you want to manipulate.
    DirectoryInfo di = new DirectoryInfo(@"c:\MyDir");

    try

    {

    // Determine whether the directory exists.

    if (di.Exists)
    {
    // Indicate that the directory already exists.

    Console.WriteLine("That path exists already.");
    return
    ;
    }

    // Try to create the directory.

    di.Create();
    Console.WriteLine(
    "The directory was created successfully."
    );

    // Delete the directory.

    di.Delete();
    Console.WriteLine(
    "The directory was deleted successfully."
    );

No comments: