The process cannot access the file 'C:\Temp\34_Temp.doc' because it is
being used by another process
I have a number of files that are created, saved, printed and then
deleted. I don't open them. The last file to be deleted is held by another
process so it can not be deleted.
This is the error message: Exception says The process cannot access the
file 'C:\Temp\34_Temp.doc' because it is being used by another process.
This is where I save the file. i use a third party control (Spire .doc for
.NET) to create the document. It is also closed after it is saved.
Dim tFname As String = My.Application.Info.DirectoryPath & "\" &
Now.ToString("hhmmss") & g.ToString() & "_Temp.doc"
OrderDetailsDoc.SaveToFile(tFname, Spire.Doc.FileFormat.Doc)
OrderDetailsDoc.Close()
SendMetoPrint(tFname)
OrderDetailsDoc = Nothing
This is the Print Procedure:
Public Sub SendMetoPrint(ByVal tmpFilename As String)
Const csSUBNAME As String = "SendMetoPrint"
On Error GoTo Errorhandler
Using dialog As New PrintDialog()
Dim d As New Spire.Doc.Document(tmpFilename)
d.PrintDialog = dialog
dialog.Document = d.PrintDocument
dialog.AllowCurrentPage = True
dialog.AllowSomePages = True
dialog.UseEXDialog = False
dialog.PrintToFile = False
dialog.Document.Print()
d.Close()
d.PrintDocument.Dispose()
d = Nothing
End Using
End Sub
This is what i use to delete the files. The thread sleep doesn't help. I
was hoping this would give the process time to release the file, but it
doesn't. Any help would be appreciated.
Public Sub ClearFolder(ByVal dirString As String)
Dim foundFile As String
Dim i As Integer
For Each foundFile In My.Computer.FileSystem.GetFiles(dirString,
FileIO.SearchOption.SearchTopLevelOnly, "*Temp.doc")
If My.Computer.FileSystem.FileExists(foundFile) Then
For i = 1 To 10
Try
My.Computer.FileSystem.DeleteFile(foundFile)
Exit For
Catch ex As Exception
If i = 10 Then
Logit(Now.ToString("MM/dd/yyyy hh:mm:ss tt") & "
" & csSUBNAME)
Logit(Now.ToString("MM/dd/yyyy hh:mm:ss tt") & "
Unable to delete file " & foundFile & " after 10
tries over 10 seconds.")
Logit(Now.ToString("MM/dd/yyyy hh:mm:ss tt") & "
Exception says: " & ex.Message)
Else
Threading.Thread.Sleep(1000)
End If
End Try
Next
End If
Next
Exit Sub
End Sub
No comments:
Post a Comment