Delete Directory (.NET)

  • TheArchitect
  • Professor
  • Professor
  • TheArchitect
  • Posts: 764
  • Loc: Australia

Post 3+ Months Ago

Hi,

I know it's possible to delete files using IO.File.Delete() in .NET but how would one delete an entire folder and all of its contents?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • TheArchitect
  • Professor
  • Professor
  • TheArchitect
  • Posts: 764
  • Loc: Australia

Post 3+ Months Ago

I guess you can use IO.Directory.Delete() but what do you do if the directory isn't empty?
  • Truce
  • Guru
  • Guru
  • Truce
  • Posts: 1477
  • Loc: Washington DC

Post 3+ Months Ago

Recursion.

Code: [ Select ]
    Dim path As String, i As Integer, files() As String

    path = "c:\path\to\directory\"

    files = System.IO.Directory.GetFiles(path)
    For i = 0 To files.Length
      Try
        System.IO.File.Delete(path & files(i))
      Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "Error occurred while deleting files!")
      End Try
    Next
    System.IO.Directory.Delete(path)
  1.     Dim path As String, i As Integer, files() As String
  2.     path = "c:\path\to\directory\"
  3.     files = System.IO.Directory.GetFiles(path)
  4.     For i = 0 To files.Length
  5.       Try
  6.         System.IO.File.Delete(path & files(i))
  7.       Catch ex As Exception
  8.         MsgBox(ex.Message, MsgBoxStyle.Critical, "Error occurred while deleting files!")
  9.       End Try
  10.     Next
  11.     System.IO.Directory.Delete(path)
  • TheArchitect
  • Professor
  • Professor
  • TheArchitect
  • Posts: 764
  • Loc: Australia

Post 3+ Months Ago

Unfortunately, the code doesn't seem to work for me. It still has a problem with deleting the directory given that it isn't empty. I've only got a few files to delete and I'm working with a different solution; deleting the files individually and then deleting the directory. I'm still open to suggestions though! Thanks. :wink:
  • mrmagu
  • Student
  • Student
  • mrmagu
  • Posts: 86
  • Loc: Great White North

Post 3+ Months Ago

TheArchitect wrote:
I've only got a few files to delete and I'm working with a different solution; deleting the files individually and then deleting the directory.


uhh, thats what truces code does... only his doesn't account for directories inside the main directory.. to combat that, just wrap that code in a function and call itself when the file is a directory.
  • katana
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 2391
  • Loc: Edinburgh, Scotland

Post 3+ Months Ago

Use Directory.Delete, not File.Delete.

i.e. (in C#):

Code: [ Select ]
string path = @"C:\my_folder_to_delete";
System.IO.Directory.Delete(path, true);
  1. string path = @"C:\my_folder_to_delete";
  2. System.IO.Directory.Delete(path, true);


Passing "true" as the second parameter tells the method to delete files & folders recursively. Hence this will delete all files and subfolders from the "my_folder_to_delete" directory and then delete the actual directory itself.
  • TheArchitect
  • Professor
  • Professor
  • TheArchitect
  • Posts: 764
  • Loc: Australia

Post 3+ Months Ago

Awesome katana. I learn something new everyday. :D
  • Jortx
  • Born
  • Born
  • Jortx
  • Posts: 1

Post 3+ Months Ago

Hello friends.
I can tell you that even passing the second parameter "True" to the function OI.Directory.Delete (), there are times when it still gives this error "The directory is not empty."
The only thing I can think of is that, knowing that the second parameter is ptrecisamente for this function can not remove the file to be retained by some process of Windows.
As sometimes happens that we try to delete a file, Windows does not allow it. I this case, I have a small program called "Unlocker" which is wonderful when I can not delete the file, open the software, I unlocked it, then I can remove it. :oops:
Genial! But then, as would be done in VB.NET, to unlock that file, and then be able to launch the IO.Directory.Delete ()???
Exlpicado have hope.
Thanks, and greetings Cuidense!
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

I've always used recursion for the files followed up by a directory.delete...

Code: [ Select ]
string[] fileList = Directory.GetFiles(TargetDirectory);
foreach (string sf in fileList)
    FileInfo(sf).Delete();
Directory.Delete(TargetDirectory);
  1. string[] fileList = Directory.GetFiles(TargetDirectory);
  2. foreach (string sf in fileList)
  3.     FileInfo(sf).Delete();
  4. Directory.Delete(TargetDirectory);
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

I've always used recursion for the files followed up by a directory.delete...

Code: [ Select ]
 
foreach (string sf in Directory.GetFiles(TargetDirectory))
    FileInfo(sf).Delete();
Directory.Delete(TargetDirectory);
 
  1.  
  2. foreach (string sf in Directory.GetFiles(TargetDirectory))
  3.     FileInfo(sf).Delete();
  4. Directory.Delete(TargetDirectory);
  5.  

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 91 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.