Delete the directory any subdirectories and files in the directory in CSharp

Description

The following code shows how to delete the directory any subdirectories and files in the directory.

Example


using System;// www. ja v  a2s  . c  o  m
using System.IO;

class Test 
{
  
    public static void Main() 
    {
        // Specify the directories you want to manipulate.
        string path = @"c:\MyDir";
        string subPath = @"c:\MyDir\temp";

        try 
        {
            Console.WriteLine("I am about to attempt to delete {0}", path);
            Directory.Delete(path, true);
            Console.WriteLine("The Delete operation was successful.");

        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        } 
        finally {}
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    IO »




File Attribute
File Security
Directory Attribute
Directory Recursive
Binary File
Text Field
Buffered IO
Create Copy Delete Move
CSV
Drive
File System Watcher
Isolated Storage
MemoryStream
Serialize
Zip