bschave2
asked on
how do i fix cannot delete file because it is being used by another process error
I have an .EXE application function function that reads files in a directory within a directory. It then pushed function to put the files in a db. then after the loadimages function does that, it loops through the directory it just loaded into the db and deletes them from the directory. Now sometimes when this exe is run there is no error, but other times there is an error. Does any body have an idea of why this is happening sometimes, but not everytime? Also, does anyone know how to fix this issue?
I am attaching the function that is receiving the error and the full error received.
FULL ERROR RECEIVED:
************ Job: Started At Mon 04/05/2010 18:30:14.89 *********
********* Step 1: Started At Mon 04/05/2010 18:30:15.00 *********
C:\>"\\msilsh0010V049\TAGJ obs\1IAM_M ailGetWork _C\MailGet Work.exe" /A ENV=Prod /A LogPath="\\msilsh0010V049\ TAGJobs\1I AM_MailGet Work_C\Log s\" /A TRN=MMTG1IAMP001,11210 /A MAILSRC=\\msilsh0010V049\T agFileNet\ MailWF
Unhandled Exception: System.IO.IOException: The process cannot access the file '\\msilsh0010V049\TagFileN et\MailWF\ ScannedIma ges\LMO201 0-04-05155 003\INDEX. TMP' because it is being used by another process.
at System.IO.__Error.WinIOErr or(Int32 errorCode, String maybeFullPath)
at System.IO.File.Delete(Stri ng path)
at MailGetWork.GetWork.delete AllFiles(S tring dir)
at MailGetWork.GetWork.LoadSc annedImage s(String Env, String MailSrc)
at MailGetWork.GetWork.Main()
C:\>ECHO OFF
********* Step 1: Finished At Mon 04/05/2010 18:31:40.42 *********
FUNCTION WHERE THE ERROR IS COMING FROM
Private Function LoadScannedImages(ByVal Env As String, ByVal MailSrc As String) As Boolean
Dim DepartmentFolder() As String
Dim FileFolder() As String
Dim Path As String
Dim i As Integer
Dim ErrorInDepartment, ErrorInScannedImages, MailDoc As Boolean
ErrorInScannedImages = False
Path = MailSrc & "ScannedImages"
DepartmentFolder = Directory.GetDirectories(P ath, "*")
For i = 0 To DepartmentFolder.Length - 1
ErrorInDepartment = False
MailDoc = True ' Some images are handled outside MailWF
If UCase(Mid(DepartmentFolder (i), InStrRev(DepartmentFolder( i), "\") + 1, 3)) = "SBP" Then MailDoc = False
If MailDoc Then
ErrorInDepartment = LoadDepartmentMail(Departm entFolder( i))
If File.Exists(DepartmentFold er(i) & "\INDEX.DAT") Then
File.Delete(DepartmentFold er(i) & "\INDEX.DAT")
End If
If ErrorInDepartment Then
ErrorInScannedImages = True
WriteToLogFile(LogFileName , "Error in folder " & DepartmentFolder(i) & ". Folder not processed.")
Else
deleteAllFiles(DepartmentF older(i))
Directory.Delete(Departmen tFolder(i) )
End If
Else
WriteToLogFile(LogFileName , "Bypassing " & DepartmentFolder(i))
End If
Next
FileFolder = Directory.GetFiles(Path, "*") ' Scanner places a file in folder with
For i = 0 To FileFolder.Length - 1 ' stats of scan. Don't need it so delete.
File.Delete(FileFolder(i))
Next
Return ErrorInScannedImages
End Function
I am attaching the function that is receiving the error and the full error received.
FULL ERROR RECEIVED:
************ Job: Started At Mon 04/05/2010 18:30:14.89 *********
********* Step 1: Started At Mon 04/05/2010 18:30:15.00 *********
C:\>"\\msilsh0010V049\TAGJ
Unhandled Exception: System.IO.IOException: The process cannot access the file '\\msilsh0010V049\TagFileN
at System.IO.__Error.WinIOErr
at System.IO.File.Delete(Stri
at MailGetWork.GetWork.delete
at MailGetWork.GetWork.LoadSc
at MailGetWork.GetWork.Main()
C:\>ECHO OFF
********* Step 1: Finished At Mon 04/05/2010 18:31:40.42 *********
FUNCTION WHERE THE ERROR IS COMING FROM
Private Function LoadScannedImages(ByVal Env As String, ByVal MailSrc As String) As Boolean
Dim DepartmentFolder() As String
Dim FileFolder() As String
Dim Path As String
Dim i As Integer
Dim ErrorInDepartment, ErrorInScannedImages, MailDoc As Boolean
ErrorInScannedImages = False
Path = MailSrc & "ScannedImages"
DepartmentFolder = Directory.GetDirectories(P
For i = 0 To DepartmentFolder.Length - 1
ErrorInDepartment = False
MailDoc = True ' Some images are handled outside MailWF
If UCase(Mid(DepartmentFolder
If MailDoc Then
ErrorInDepartment = LoadDepartmentMail(Departm
If File.Exists(DepartmentFold
File.Delete(DepartmentFold
End If
If ErrorInDepartment Then
ErrorInScannedImages = True
WriteToLogFile(LogFileName
Else
deleteAllFiles(DepartmentF
Directory.Delete(Departmen
End If
Else
WriteToLogFile(LogFileName
End If
Next
FileFolder = Directory.GetFiles(Path, "*") ' Scanner places a file in folder with
For i = 0 To FileFolder.Length - 1 ' stats of scan. Don't need it so delete.
File.Delete(FileFolder(i))
Next
Return ErrorInScannedImages
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Faced a similar problem some times back. I moved the document to a temporary folder and deleted from the original folder. After processing I deleted from the temporary folder. The reason why it fails to delete sometimes is that the system could still be referring to it- perhaps you can disconnect from the database completely.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it's a false error. The directory cannot be delted if there are files in the directory. Specifically .tmp files. So, I just through a try and catch in there because if the directory doesn't delete the other automated program knows to just add more files into the directory rather than create a whole new directory. Thanks for the help though.