Author Topic: C# - Delete Directory When it is not empty - deltree command  (Read 3013 times)


  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
C# - Delete Directory When it is not empty - deltree command
« on: December 01, 2011, 06:35:49 pm »
Directory.Delete function takes two arguments - directory path and true or false. Default value is false. By default, it will delete the specified directory, if it is empty. If it is not empty, then it throws an exception.  If you pass true as second argument, then it deletes all files and sub directories recursively. But you need write permission on this folder. Otherwise you would get System.IO.IOException

Code: (C#)
//Source Code Listing

using System;
using System.IO;
namespace ListDirectory
   class Program
     static void Main(string[] args)
         string[] arrTempSubDirs = Directory.GetDirectories("c:\\kathir", "b*", SearchOption.TopDirectoryOnly);
         foreach (string dir in arrTempSubDirs)
            Directory.Delete(dir, true); // true applies to delete recursively all sub directories and files.


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1