C# Directory Delete(String)

Description

Directory Delete(String) Deletes an empty directory from a specified path.

Syntax

Directory.Delete(String) has the following syntax.


public static void Delete(
  string path
)

Parameters

Directory.Delete(String) has the following parameters.

  • path - The name of the empty directory to remove. This directory must be writable and empty.

Returns

Directory.Delete(String) method returns

Example

The following example shows how to create a new directory and subdirectory, and then delete only the subdirectory.


using System;/*www.  j a  va2s  . c  o  m*/
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string subPath = @"C:\NewDirectory\NewSubDirectory";

        Directory.CreateDirectory(subPath);   
        Directory.Delete(subPath);

        bool directoryExists = Directory.Exists(@"C:\NewDirectory");
        bool subDirectoryExists = Directory.Exists(subPath);

        Console.WriteLine("top-level directory exists: " + directoryExists);
        Console.WriteLine("sub-directory exists: " + subDirectoryExists);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter