i have an application that create a txt file and write in it a log. I create the txt file with the following code:
If System.IO.File.Exists(sFileName) = True Then
System.IO.File.Delete(sFileName)
End If
'System.IO.File.Create(sFileName) '.dispose
Dim objWriter As New System.IO.StreamWriter(sFileName, True)
When i finish to write the log (using objWriter.WriteLine) and close it (objwriter.close and objwriter.dispose), send it by mail and need to delete it.
In order to delete the file i use this code:
For i = 0 To 10
Try
'System.IO.File.Delete(sFileName)
'My.Computer.FileSystem.DeleteFile(sFileName, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
My.Computer.FileSystem.DeleteFile(sFileName)
Exit For
Catch ex As Exception
If i = 10 Then
invioMailTest(ex.ToString)
End If
Threading.Thread.Sleep(1000)
End Try
Next
The code works well in local, but when i run it on the server (as administrator) it gives me the following error:
System.IO.IOException: The process cannot access the file 'C:\Log_Eventi\Export log 2016-04-13.txt' because it is being used by another process.
i don't know how to delete it and i'm losing too much time on it....