deleting directory???

tahu191

Well-known member
Joined
Oct 27, 2005
Messages
48
Location
USA
Programming Experience
Beginner
I need to know how to delete a directory or folder on a computer using VB 2005
 
cjard said:
Juggalo; does that work even if there are files in the directory?
Why not try it and see? The method is overloaded and Intellisense will tell you what you need to do. We all have two hands.
 
when i was at university, i would occasionally ask questions not because I was personally seeking the answer, but because I could reasonably see that someone else would want to know..

i appreciate that you would want to encourage people to think for themselves, look things up in msdn etc but in some cases, the answer is quick enough to deliver when asked - it makes this thread a better source of reference :)

so, the answer to my question (for the benefit of the OP) is:

actually System.IO.Directory.Delete() doesnt exist. because Directory is a static class, it doesnt actually represent a specific directory on the system and hence you must supply a path and optionally a boolean indicating whether the delete should recursively clear the directory or not. (no accessible delete takes 0 arguments)

by contrast, in Object Oriented terms, System.IO.DirectoryInfo is an instantiable class that would represent a single directory on the system. Once such an instance existed, Delete() can be invoked, but the directory it pertains to must be empty. Delete(true) can be invoked to recursively clear the directory :)
 
tahu191, since your using VB.Net 2.0 you can use the 'My' shortcut to the my.Computer.FileSystem.DeleteDirectory method. It is basically a shortcut to the System.IO.Directory.Delete method.
 
JohnH said:
tahu191, since your using VB.Net 2.0 you can use the 'My' shortcut to the my.Computer.FileSystem.DeleteDirectory method. It is basically a shortcut to the System.IO.Directory.Delete method.
Actually it's more than that. The My.Computer.FileSystem object does use members of System.IO in some circumstances but it also wraps many calls to the Windows API, which is how it is able to display progress dialogues during various operations. All overloads of DeleteDirectory call the private DeleteDirectoryInternal method. Here's how it's implemented:
VB.NET:
[COLOR=#1000a0]Private[/COLOR] [COLOR=#1000a0]Shared[/COLOR] [COLOR=#1000a0]Sub[/COLOR] [B]DeleteDirectoryInternal[/B]([COLOR=#1000a0]ByVal[/COLOR] [B]directory[/B][COLOR=#1000a0] As [/COLOR][URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=1"]String[/URL], [COLOR=#1000a0]ByVal[/COLOR] [B]onDirectoryNotEmpty[/B][COLOR=#1000a0] As [/COLOR][URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=2"]DeleteDirectoryOption[/URL], [COLOR=#1000a0]ByVal[/COLOR] [B]showUI[/B][COLOR=#1000a0] As [/COLOR][URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=3"]UIOptionInternal[/URL], [COLOR=#1000a0]ByVal[/COLOR] [B]recycle[/B][COLOR=#1000a0] As [/COLOR][URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=4"]RecycleOption[/URL], [COLOR=#1000a0]ByVal[/COLOR] [B]onUserCancel[/B][COLOR=#1000a0] As [/COLOR][URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=5"]UICancelOption[/URL])
      [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=6"]FileSystem[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=7"]VerifyDeleteDirectoryOption[/URL]([COLOR=#800000]"onDirectoryNotEmpty"[/COLOR], [URL="http://www.vbdotnetforums.com/"]onDirectoryNotEmpty[/URL])
      [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=8"]FileSystem[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=9"]VerifyRecycleOption[/URL]([COLOR=#800000]"recycle"[/COLOR], [URL="http://www.vbdotnetforums.com/"]recycle[/URL])
      [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=10"]FileSystem[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=11"]VerifyUICancelOption[/URL]([COLOR=#800000]"onUserCancel"[/COLOR], [URL="http://www.vbdotnetforums.com/"]onUserCancel[/URL])
      [COLOR=#1000a0]Dim[/COLOR] [B]text1[/B] [COLOR=#1000a0]As[/COLOR] [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=12"]String[/URL] = [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=13"]Path[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=14"]GetFullPath[/URL]([URL="http://www.vbdotnetforums.com/"]directory[/URL])
      [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=15"]FileSystem[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=16"]DemandDirectoryPermission[/URL]([URL="http://www.vbdotnetforums.com/"]text1[/URL], [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=17"]FileIOPermissionAccess[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=18"]Write[/URL])
      [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=19"]FileSystem[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=20"]ThrowIfDevicePath[/URL]([URL="http://www.vbdotnetforums.com/"]text1[/URL])
      [COLOR=#1000a0]If[/COLOR] [COLOR=#1000a0]Not[/COLOR] [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=21"]Directory[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=22"]Exists[/URL]([URL="http://www.vbdotnetforums.com/"]text1[/URL]) [COLOR=#1000a0]Then[/COLOR]
            [COLOR=#1000a0]Throw[/COLOR] [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=23"]ExceptionUtils[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=24"]GetDirectoryNotFoundException[/URL]([COLOR=#800000]"IO_DirectoryNotFound_Path"[/COLOR], [COLOR=#1000a0]New[/COLOR] [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=25"]String[/URL]() { [URL="http://www.vbdotnetforums.com/"]directory[/URL] })
      [COLOR=#1000a0]End If[/COLOR]
      [COLOR=#1000a0]If[/COLOR] [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=26"]FileSystem[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=27"]IsRoot[/URL]([URL="http://www.vbdotnetforums.com/"]text1[/URL]) [COLOR=#1000a0]Then[/COLOR]
            [COLOR=#1000a0]Throw[/COLOR] [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=28"]ExceptionUtils[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=29"]GetIOException[/URL]([COLOR=#800000]"IO_DirectoryIsRoot_Path"[/COLOR], [COLOR=#1000a0]New[/COLOR] [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=30"]String[/URL]() { [URL="http://www.vbdotnetforums.com/"]directory[/URL] })
      [COLOR=#1000a0]End If[/COLOR]
      [COLOR=#1000a0]If[/COLOR] (([URL="http://www.vbdotnetforums.com/"]showUI[/URL] <> [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=31"]UIOptionInternal[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=32"]NoUI[/URL]) [COLOR=#1000a0]AndAlso[/COLOR] [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=33"]Environment[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=34"]UserInteractive[/URL]) [COLOR=#1000a0]Then[/COLOR]
            [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=35"]FileSystem[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=36"]ShellDelete[/URL]([URL="http://www.vbdotnetforums.com/"]text1[/URL], [URL="http://www.vbdotnetforums.com/"]showUI[/URL], [URL="http://www.vbdotnetforums.com/"]recycle[/URL], [URL="http://www.vbdotnetforums.com/"]onUserCancel[/URL], [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=37"]FileOrDirectory[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=38"]Directory[/URL])
      [COLOR=#1000a0]Else[/COLOR]
            [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=39"]Directory[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=40"]Delete[/URL]([URL="http://www.vbdotnetforums.com/"]text1[/URL], ([URL="http://www.vbdotnetforums.com/"]onDirectoryNotEmpty[/URL] = [URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=41"]DeleteDirectoryOption[/URL].[URL="http://www.aisto.com/roeder/dotnet/Default.aspx?Object=42"]DeleteAllContents[/URL]))
      [COLOR=#1000a0]End If[/COLOR]
[COLOR=#1000a0]End Sub[/COLOR]
The ShellDelete method that is called under some circumstances calls Windows API functions. The same ones that are used when you delete a folder in Windows Explorer.

The two links that I posted originally were the results of searches on MSDN for (delete directory) and (delete directory "visual basic"). Each of those has links to information on System.IO.Directory.Delete and My.Computer.FileSystem.DeleteDirectory in five of the top six results, and others besides.
 
Back
Top