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.