0

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....

P.Oliva
  • 1
  • 1
  • http://stackoverflow.com/a/26741192/2118383 – Vignesh Kumar A Apr 13 '16 at 11:01
  • 1
    Possible duplicate of [IOException: The process cannot access the file 'file path' because it is being used by another process](http://stackoverflow.com/questions/26741191/ioexception-the-process-cannot-access-the-file-file-path-because-it-is-being) – Vignesh Kumar A Apr 13 '16 at 11:01
  • Depends on which process is using it. If it's your program, then make sure you properly close the file all the time. If it's an other program, then that will be harder. Also, you don't need to delete the file right before opening it, just don't open it as append. – the_lotus Apr 13 '16 at 11:34
  • only by this program wich create the txt file, send it by mail and then should delete it. i developed a program that delete only that file and it works fine. The problem is to delete the file in the same program that generate it. – P.Oliva Apr 13 '16 at 11:45
  • Sleep 2-3 seconds before deletion - this way anitviruses and other background processes can scan and release it. Of course you must be sure that your code releases it properly first. – Arvo Apr 13 '16 at 12:01
  • If you are only writing the data to disk to email it as an attachment, note that you can also use a memorystream to create an attachment. Otherwise, I suggest that you delete log files from *earlier* than the one you send. – Andrew Morton Apr 13 '16 at 12:06

2 Answers2

0

When you create your file I'm guessing you are using

System.IO.File.Create(sFileName)

You can overcome that problem by using this code:

Using File.Create(sFileName)

End Using

put all code that deals with sFileName within Using.

other solutions: use Close when you create it

System.IO.File.Create(sFileName).Close()

use filestream

Dim fs As FileStream = File.Create(sFileName)
do your work here
fs.Close()
Claudius
  • 1,883
  • 2
  • 21
  • 34
  • i tried all your solutions, but i still have the problem. i don't have any other ideas, i tried to put a sleep statement of 10 seconds after the file.dispose and other 20 seconds after the send of the e-mail. But file still in use... i've tried to create the file with the following commands: - System.IO.File.Create(sFileName).Close() - with the filestream -and the using statement – P.Oliva Apr 13 '16 at 14:13
  • can i see how you create this file – Claudius Apr 13 '16 at 14:14
  • maybe instead of objectWriter just use System.IO.File.AppendAllText. It is also somehow hard to believe that using didn't work – Claudius Apr 13 '16 at 14:25
0

You mention "send it by mail", so the problem is likely that the attachment is still being held by the server's email system.

Mark Hurd
  • 10,665
  • 10
  • 68
  • 101