473,796 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(c#) delete all files and folders in a directory recursively

7 New Member
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.delet e at the same time.
Jun 21 '07 #1
2 13263
dip_developer
648 Recognized Expert Contributor
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.delet e at the same time.
try this...

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim s As String
  3. For Each s In System.IO.Directory.GetFiles("c:\temp")
  4. System.IO.File.Delete(s)
  5. Next s 
  6.  
Expand|Select|Wrap|Line Numbers
  1.  string s;
  2. foreach (s in System.IO.Directory.GetFiles("c:\\temp"))
  3.  {
  4.      System.IO.File.Delete(s);
  5.  }
  6.  
Jun 21 '07 #2
halimunan
7 New Member
owh.. good.. it works... thanks man!

it deletes all the files but not folders.. and this maybe due to the "read only" on the folders...
how do i change this? directory.SetAc cessControl?

because everytime i tried to change the status.. it will then become "read only" again...
Jun 21 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
15670
by: Ben | last post by:
Greetings, I am looking for a way to search for and delete files based on a pattern mask. For example, the search method would find all files matching a certain pattern containing wildcards (e.g. *FILE29*.TXT). I'm looking for a way to do this in either Visual Basic or C(++). Thanks!
8
1937
by: Adam Clauss | last post by:
I have a folder containing many subfolders (and subfolders and....) all containing various .cs files. Is there any "easy" way to get them all added to the solution. Preferable would be that the folders are actually created in the Solution Explorer so that I can find things easily. Its easy to select multiple files out of a single folder, but not recursively into subfolders. Any ideas? -- Adam Clauss
1
13130
by: genc ymeri | last post by:
Hi over there, What command should I run to delete folders even if theirs subfolders are not empty with files ????? Thanks in advance.
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:
2
2411
by: Cesar Ronchese | last post by:
Hello, I'm experiencing a very weird problem. I have a ASP.Net 2005 application (VB.Net) that creates some folders to store temporary files. example: Session_Start(...) IO.Directory.CreateDirectory(Server.MapPath(".") & "\SessionFolders\" & Session.SessionID) (...) The same application holds a session object that logs on to my server application (desktop - non ASP), that shows all connected clients in a listview. See sample code:
10
2925
by: Dan | last post by:
Hi - I'm about a week into learning VB.NET, and I'm finding I can't delete any of the VB.NET directory structures that contain my test projects I've been trying to create. I've never seen this before... Whenever I try, it says something like "access is denied" and "make sure the disk is not write-protected and file is not currently in use". Is there something VB.NET does that would cause this? I'm starting to build up a bunch of test...
3
3390
by: Matt | last post by:
I have several folders on a server that contains files that go back 3-4 years. It was mentioned that keeping files that long is no longer needed. So my question is, is there a way to create a windows service in C# that can delete files in the folders that are older then 12 months? The server is structured something like this: DataLogs Server1 App1 - this folder contains all the log files or it can look something like this: DataLogs
0
2957
by: wolfsbane | last post by:
Alright, here it is I am trying to write a win32 app in VB 2005 to clean up user's profiles. everything works correctly except for the Delete("directory", True) statement. I get a System.IO.DirectoryNotFoundException: Could not find a part of the path. Then it gives me a random file name from the UserProfile\Local Settings\Temporary Internet Files\Content.IE5\SomeRandomFolder. This is absolutley driving me nuts. So I am gonn give you a basic...
0
9685
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...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10467
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10201
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
9061
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
7558
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
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4130
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

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.