Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

How to recursively delete a directory in .NET that contains files

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dave - 09 Jan 2008 21:59 GMT
Directory.Delete(path, true (recursive)) throws an exception of the
directory is not empty.  silly as they could have overloaded the
method further providing for deletions with files...

anyway, i also tried using Process and spawning a DOS rmdir command
but that it quite inelegant.

any ideas as to how to cleanly do this?

thanks, dave
Marc Gravell - 09 Jan 2008 23:29 GMT
> any ideas as to how to cleanly do this?
Delete the files and subdirs first? Not tidy, I know... but there
t'is.

Marc
Alberto Poblacion - 10 Jan 2008 06:35 GMT
> Directory.Delete(path, true (recursive)) throws an exception of the
> directory is not empty.  silly as they could have overloaded the
[quoted text clipped - 4 lines]
>
> any ideas as to how to cleanly do this?

   private static void BorrarRecursivamente(string ruta)
   {
       string[] ficheros = Directory.GetFiles(ruta);
       foreach (string fichero in ficheros) File.Delete(fichero);
       string[] directorios = Directory.GetDirectories(ruta);
       foreach (string directorio in directorios)
       {
           BorrarRecursivamente(directorio);
       }
       Directory.Delete(ruta);
   }
Willy Denoyette [MVP] - 10 Jan 2008 11:56 GMT
> Directory.Delete(path, true (recursive)) throws an exception of the
> directory is not empty.  silly as they could have overloaded the
[quoted text clipped - 6 lines]
>
> thanks, dave

Easy and fast using System.Management.

 string dirName = @"c:\\somefolder";
 string objPath = string.Format("Win32_Directory.Name='{0}'",dirName);
 using (ManagementObject dir= new ManagementObject(objPath))
 {
  ManagementBaseObject outParams = dir.InvokeMethod("Delete", null, null);
  uint ret = (uint)(outParams.Properties["ReturnValue"].Value);
  if(ret == 0)
   Console.WriteLine("Success");
  else Console.WriteLine("Failed with error code: {0}", ret);
 }

Willy.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.