473,803 Members | 3,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Directory.Delet e(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
Jan 9 '08 #1
3 4538
any ideas as to how to cleanly do this?
Delete the files and subdirs first? Not tidy, I know... but there
t'is.

Marc
Jan 9 '08 #2
"Dave" <dj*********@gm ail.comwrote in message
news:0a******** *************** ***********@q39 g2000hsf.google groups.com...
Directory.Delet e(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?

private static void BorrarRecursiva mente(string ruta)
{
string[] ficheros = Directory.GetFi les(ruta);
foreach (string fichero in ficheros) File.Delete(fic hero);
string[] directorios = Directory.GetDi rectories(ruta) ;
foreach (string directorio in directorios)
{
BorrarRecursiva mente(directori o);
}
Directory.Delet e(ruta);
}
Jan 10 '08 #3
"Dave" <dj*********@gm ail.comwrote in message
news:0a******** *************** ***********@q39 g2000hsf.google groups.com...
Directory.Delet e(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


Easy and fast using System.Manageme nt.

string dirName = @"c:\\somefolde r";
string objPath = string.Format(" Win32_Directory .Name='{0}'",di rName);
using (ManagementObje ct dir= new ManagementObjec t(objPath))
{
ManagementBaseO bject outParams = dir.InvokeMetho d("Delete", null, null);
uint ret = (uint)(outParam s.Properties["ReturnValu e"].Value);
if(ret == 0)
Console.WriteLi ne("Success");
else Console.WriteLi ne("Failed with error code: {0}", ret);
}
Willy.

Jan 10 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3148
by: Ruby Tuesday | last post by:
Is there a fast way to read files/directory recursively? Instead of inspecting each file(s)/dir(s), is there a way to know that its a file or a directory from its hidden attribut both for windows or unix filesystem? Thanks
1
3360
by: Antonio Lopez Arredondo | last post by:
hi all !!! I need to copy a folder and its subfolders to another location; which class should I use ? could only find the System.IO.Directory.MOVE but don't know how to COPY. thanks in advance, ant
2
10605
by: melo | last post by:
Hello, I've been struggling with a function(s) to recursively set all folders and files to NOT read-only. So, I thought I'd post this message. What I need to do is: given a starting path, I need to recursively go through all files and folders below the starting path and check if the file or folder is read-only and, if so, set it to not read-only. Any ideas?
0
1440
by: Arjan van den Noort | last post by:
How to delete a directory (with all containing directories and files) with vb.net if the directory contains long paths ? (just like the dos command: RD /S/Q path) What I want is a routine to delete files and folders from a shared folder (accessed by its UNC-notation). That's all ! Some path names can be long (> 200 characters ) and there are some files and folders that can be read-only. The exceptions I get:
5
655
by: rbt | last post by:
What is the most efficient way to recursively remove files and directories? Currently, I'm using os.walk() to unlink any files present, then I call os.walk() again with the topdown=False option and get rid of diretories with rmdir. This works well, but it seems that there should be a more efficient way. Here are my function definitions: def remove_files(target_dir): # This attempts to remove _all_ files from a directory. # Use with...
2
2938
by: Zytan | last post by:
You can download them here: http://msdn2.microsoft.com/en-us/vstudio/aa718338.aspx This snippet seems wrong: Visual C# 2005 Code Snippets -filesystem -Search a Directory for Files Recursively I don't believe DirSearch will return any results for files in the root directory originally passed to it. Am I right? Here's the code:
2
13265
by: halimunan | last post by:
hello all, how do i delete all folders and files inside a directory while at the same time keeping(not deleting the directory) ? e.g : i want to delete all folders and files inside c:\temp it seems like i need to use file.delete and directory.delete at the same time.
9
2418
by: bhumikas | last post by:
Hi all, I need a help in perl script.The basic idea is,it must have command line arguments for the user flexibility.the files are in the format as shown below. MainFolder Directory Sub_directory files Sub_directory files Directory
1
6445
by: rahullko05 | last post by:
I am try to write a program which can recursively traverse all files and if it finds directory then it goes inside that sub directory and list all files of that subdirectory, again comes back to main directory to traverse rest of the files... Problem i am facing is, its just going inside first sub directory it comes across and then comes back to main directory, and treats all other files as files (even if its a directory). i am not able to...
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10300
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9127
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7607
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5503
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2974
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.