From: Scott McNair on 13 Oct 2009 13:30 Is there a way to "instantly" delete a folder and all subfolders? I'm currently trying: System.IO.Directory.Delete(Filepath, True) Each of these folders contains 60 subfolders which in turn contain about 100 files each (each of which is about 180kb). Because of this, my application grinds to a halt. Is there a way to simply flag a folder and its contents as deleted without having to go through the recurse?
From: Alex Clark on 13 Oct 2009 14:22 Well, you could change the directory's "Hidden" attribute to make it less visible, but that won't really solve it. Unfortunately the only way to really delete something... is to delete it. But if your app is grinding to a halt, why not just run the delete op on a background worker thread? "Scott McNair" <smcnair(a)beachexpress.takethispartout.com> wrote in message news:Xns9CA37F46A3EF0donquixote235gmailco(a)207.46.248.16... > Is there a way to "instantly" delete a folder and all subfolders? I'm > currently trying: > > System.IO.Directory.Delete(Filepath, True) > > Each of these folders contains 60 subfolders which in turn contain about > 100 files each (each of which is about 180kb). Because of this, my > application grinds to a halt. > > Is there a way to simply flag a folder and its contents as deleted without > having to go through the recurse?
From: Miro on 13 Oct 2009 16:55 I have never tried this... but you got me thinking...what about not "deleting" but moving the folder to the recycling bin.... I found this... (again - I have never tried this, and do not know if this will work with folders) http://visualbasic.about.com/b/2006/12/22/recycle-files-with-vbnet-2005.htm Miro "Scott McNair" <smcnair(a)beachexpress.takethispartout.com> wrote in message news:Xns9CA37F46A3EF0donquixote235gmailco(a)207.46.248.16... > Is there a way to "instantly" delete a folder and all subfolders? I'm > currently trying: > > System.IO.Directory.Delete(Filepath, True) > > Each of these folders contains 60 subfolders which in turn contain about > 100 files each (each of which is about 180kb). Because of this, my > application grinds to a halt. > > Is there a way to simply flag a folder and its contents as deleted without > having to go through the recurse?
From: Göran Andersson on 13 Oct 2009 18:22 Scott McNair wrote: > Is there a way to "instantly" delete a folder and all subfolders? I'm > currently trying: > > System.IO.Directory.Delete(Filepath, True) > > Each of these folders contains 60 subfolders which in turn contain about > 100 files each (each of which is about 180kb). Because of this, my > application grinds to a halt. > > Is there a way to simply flag a folder and its contents as deleted without > having to go through the recurse? No, the files are not really stored inside the folder on the disk, so you can't remove them as a single unit. If you would remove the folder without first deleting each file in it, the files would become orphaned occupied space that you could neither reach nor remove. The files would be lost, but you would not get the disk space back. -- G�ran Andersson _____ http://www.guffa.com
From: Michael D. Ober on 13 Oct 2009 19:53 "Scott McNair" <smcnair(a)beachexpress.takethispartout.com> wrote in message news:Xns9CA37F46A3EF0donquixote235gmailco(a)207.46.248.16... > Is there a way to "instantly" delete a folder and all subfolders? I'm > currently trying: > > System.IO.Directory.Delete(Filepath, True) > > Each of these folders contains 60 subfolders which in turn contain about > 100 files each (each of which is about 180kb). Because of this, my > application grinds to a halt. > > Is there a way to simply flag a folder and its contents as deleted without > having to go through the recurse? > Without testing, I'd give this a try. There is no way to "instantly" remove a folder tree in Windows. ' Option Compare Text' not needed as there are no string comparisons Option Strict On Option Explicit On Imports System.Threading Imports System.IO Module RecursiveDelete Public Function rmdir(ByVal rootToDelete As String) As Thread Dim th As New Thread(AddressOf thDelete) th.IsBackground = False ' Set to false if you want this thread to continue after the program "ends". ' Actually, this will really keep the program alive in memory until this thread has ended th.Start(rootToDelete) Return th End Function Private Sub thDelete(ByVal objRootToDelete As Object) ' dot net purists will hate this error handler, but this is quick and dirty; ' If you need to know if a deletion fails, you'll need to use a try/catch block. ' In either case, you need some sort of error handler or this thread will bring down ' your application if a deletion fails. ' If a deletion fails, continue. The directory deletes will also fail appropriately. On Error Resume Next ' Convert the obj variable to a string Dim rootToDelete As String = CStr(objRootToDelete) ' do the files first for underlying file system performance - OP stated 100 files vs 60 folders. ' Get the bigger group first to reduce the scan time for the smaller group. For Each f As String In Directory.GetFiles(rootToDelete) File.Delete(f) Next f ' Recurse into each folder For Each d As String In Directory.GetDirectories(rootToDelete) thDelete(rootToDelete) Next d ' Get the folder itself Directory.Delete(rootToDelete) End Sub End Module Given how the directory functions work in dotnet, I suspect doing the double loop like this is quicker than using the My namespace function. By deleting the files first, you reduce the time it will take to process the directory for the sub dir removal. If you're willing to use the FindFirstFile() API you can get directories and files in a single loop, but it will take significantly longer to write and test that code (unless you're an old Win32/C++ programmer) The reason I made the entry point rmdir a function is so that you can easily test if the thread is still running with a th.isAlive test or wait for it to complete with a th.Join. Mike.
|
Next
|
Last
Pages: 1 2 3 Prev: Connecting to a WAN Next: Datagridview -- problems in Backgroudworker |