N
noctufaber
I wrote a recursive file/folder copy/delete routine because someone on
the 2.0 .NET development team was too lazy to make directory.move work
across volumes (i.e. moving a folder from drive C: to drive D.
Anyway, everything works great with the copy routine. The problem
comes up during the File.Delete. Well over a hundred files are
successfully deleted by my routine except for one file. I have
checked the file's security/permission settings and they are IDENTICAL
to the files that successfully delete. I put DoEvents and
Thread.Sleep commands, but they don't work either.
Has anybody seen this and found a solution? My source code is shown
below:
--------------------------------------------------------------------------
Sub DeleteDirectory(ByVal theSrcDir As String)
Dim theDirList() As String =
Directory.GetDirectories(theSrcDir)
Dim dirparts() As String = theSrcDir.Split("\")
Dim trueDest As String = driveLetter
For i As Integer = srcRootIdx To dirparts.Length - 1
trueDest = trueDest & "\" & dirparts(i)
Next
' srcRoot & theSrcDir.Substring(theSrcDir.Length -
srcRoot.Length + 2)
For Each DirItem As String In theDirList
DeleteDirectory(DirItem)
Next
Dim theFileList() As String = Directory.GetFiles(theSrcDir)
For Each FileItem As String In theFileList
Dim BaseNameParts() As String = FileItem.Split("\")
If (Not Directory.Exists(trueDest)) Then
Directory.CreateDirectory(trueDest)
Try
Dim times As Integer = 0
Do
If times > 10 Then Throw New Exception("Stupid
Windows Can't delete a file!!!!!!!")
Try
File.Delete(FileItem) ' HERE IS THE ANNOYING,
FRUSTRATING PROBLEM!!!
Application.DoEvents()
Threading.Thread.Sleep(1000)
Catch ex As Exception
MsgBox(ex.Message)
End Try
times += 1
Loop Until (Not File.Exists(FileItem))
Catch
End Try
Next
Try
Directory.Delete(theSrcDir)
Catch
End Try
End Sub
the 2.0 .NET development team was too lazy to make directory.move work
across volumes (i.e. moving a folder from drive C: to drive D.
Anyway, everything works great with the copy routine. The problem
comes up during the File.Delete. Well over a hundred files are
successfully deleted by my routine except for one file. I have
checked the file's security/permission settings and they are IDENTICAL
to the files that successfully delete. I put DoEvents and
Thread.Sleep commands, but they don't work either.
Has anybody seen this and found a solution? My source code is shown
below:
--------------------------------------------------------------------------
Sub DeleteDirectory(ByVal theSrcDir As String)
Dim theDirList() As String =
Directory.GetDirectories(theSrcDir)
Dim dirparts() As String = theSrcDir.Split("\")
Dim trueDest As String = driveLetter
For i As Integer = srcRootIdx To dirparts.Length - 1
trueDest = trueDest & "\" & dirparts(i)
Next
' srcRoot & theSrcDir.Substring(theSrcDir.Length -
srcRoot.Length + 2)
For Each DirItem As String In theDirList
DeleteDirectory(DirItem)
Next
Dim theFileList() As String = Directory.GetFiles(theSrcDir)
For Each FileItem As String In theFileList
Dim BaseNameParts() As String = FileItem.Split("\")
If (Not Directory.Exists(trueDest)) Then
Directory.CreateDirectory(trueDest)
Try
Dim times As Integer = 0
Do
If times > 10 Then Throw New Exception("Stupid
Windows Can't delete a file!!!!!!!")
Try
File.Delete(FileItem) ' HERE IS THE ANNOYING,
FRUSTRATING PROBLEM!!!
Application.DoEvents()
Threading.Thread.Sleep(1000)
Catch ex As Exception
MsgBox(ex.Message)
End Try
times += 1
Loop Until (Not File.Exists(FileItem))
Catch
End Try
Next
Try
Directory.Delete(theSrcDir)
Catch
End Try
End Sub