none
Attempting to delete a folder gives a "Not empty" error even when told to "DeleteAllContents" RRS feed

  • Question

  • This is ticking me off. I'm trying to delete a folder that may or may not contain data. So I issue the following command:

    FileIO.FileSystem.DeleteDirectory("c:\MyFolder", FileIO.DeleteDirectoryOption.DeleteAllContents)

    .
    But when I run my program, it appeared the command was being ignored (no error, it was simply skipped.) So I put it in a Try statement:

    Try
       FileIO.FileSystem.DeleteDirectory("c:\MyFolder", FileIO.DeleteDirectoryOption.DeleteAllContents)
    Catch ex As Exception
       MsgBox(ex.Message,, "Error")
    End Try

    .
    And this catches an undetected error with the message "Directory not empty."

    I know that. That's why I included ".DeleteAllContents". What am I missing?

    TIA


    Tuesday, May 14, 2019 7:09 PM

Answers

  • Have you tried Directory.Delete which has two constructors?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, May 14, 2019 7:18 PM
    Moderator
  • Hi

    Karen put a link un her post.

    You would need:

    IO.Directory.Delete("path As String", True)
    
    where the second parameter is for 'recursive'


    Regards Les, Livingston, Scotland

    Tuesday, May 14, 2019 8:07 PM

All replies

  • Have you tried Directory.Delete which has two constructors?

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, May 14, 2019 7:18 PM
    Moderator
  • Have you tried Directory.Delete which has two constructors?

    .

    Thanks for the reply.

    What is "Directory.Delete" a method of? I'm finding no such command in VB. It's not part of "FileIO" nor "My.Computer".

    Tuesday, May 14, 2019 7:57 PM
  • Hi

    Karen put a link un her post.

    You would need:

    IO.Directory.Delete("path As String", True)
    
    where the second parameter is for 'recursive'


    Regards Les, Livingston, Scotland

    Tuesday, May 14, 2019 8:07 PM
  • Have you tried Directory.Delete which has two constructors?

    .

    Thanks for the reply.

    What is "Directory.Delete" a method of? I'm finding no such command in VB. It's not part of "FileIO" nor "My.Computer".

    As Les pointed out, Directory.Delete is a link in my reply.

    Imports System.IO
    
    Module Module1
    
        Sub Main()
            Dim subPath = "C:\NewDirectory\NewSubDirectory"
    
            Try
                Directory.CreateDirectory(subPath)
                Directory.Delete(subPath,True)
    
                Dim directoryExists = Directory.Exists("C:\NewDirectory")
                Dim subDirectoryExists = Directory.Exists(subPath)
    
                Console.WriteLine("top-level directory exists: " & directoryExists)
                Console.WriteLine("sub-directory exists: " & subDirectoryExists)
    
            Catch e As Exception
                Console.WriteLine("The process failed: {0}", e.Message)
            End Try
        End Sub
    
    End Module


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, May 14, 2019 8:14 PM
    Moderator
  • Karen put a link un her post.


    You would need:

    IO.Directory.Delete("path As String", True)
    where the second parameter is for 'recursive'

    .
    Thanks for the reply. Karen's link only gives an example in C#.

    This method appears to work (more trials will confirm), though it is frustrating not knowing why my method failed.

    I don't know who should get credit for the answer, so credit will go to which of you can figure out why the method I used produced an error. ;)


    Tuesday, May 14, 2019 8:36 PM
  • Give the credit to Karen - she needs the points :)

    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, May 14, 2019 8:53 PM
    Tuesday, May 14, 2019 8:52 PM

  • I don't know who should get credit for the answer, so credit will go to which of you can figure out why the method I used produced an error. ;)

    Hi

    It is very difficult to guess the reason for your issue as the EXACT code from your post works perfectly here.


    Regards Les, Livingston, Scotland

    Tuesday, May 14, 2019 9:05 PM
  • I talked to Microsoft about this, see at the top of the page the following where in your case it might default to C# but click it to get VB.NET code, kind of frustrating as you can see.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, May 14, 2019 9:22 PM
    Moderator

  • I don't know who should get credit for the answer, so credit will go to which of you can figure out why the method I used produced an error. ;)


    You do know you mark multiple replies as answered thus in this case if you feel compelled mark Les as a secondary answer.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, May 14, 2019 9:24 PM
    Moderator
  • It is very difficult to guess the reason for your issue as the EXACT code from your post works perfectly here.

    .
    I have a theory. The problem seems to occur when I have the Windows File Explorer open to my C: drive.

    Closing Explorer seems to resolve the issue. But having to check to see if the user has an app open that's viewing the C: drive and then alerting them to close it, would have been a huge PIA.

    Thankfully, this alternate method doesn't seem to care.

    Tuesday, May 14, 2019 9:36 PM