I'm having problems working with a streamwriter object. After closing the
streamwriter and setting it to Nothing, I try to delete the file it was
writing to, but I always get the following error message:
"The process cannot access the file "whatever" because it is being used
by another process."
I've even tried opening another file using the same streamwriter object
before deleting the original file, but it's no use. Something keeps a lock
on that original file, but I can't figure out what.
To reproduce this bug, create a new Windows Application project and create
the following class:
--- START ---
Imports System.IO
Public Class StreamWriterTest
#Region " Constructor/Destructor "
Public Sub New(ByVal filename As String)
' Remember the filename
_filename = filename
' Determine the path we should be storing the log file in
_folder =
System.Environment.ExpandEnvironmentVariables("%Wi nDir%") & "\Temp\"
' Open the log file
OpenLogFile()
End Sub
Protected Overrides Sub Finalize()
' Close the log file
CloseLogFile()
MyBase.Finalize()
End Sub
#End Region
#Region " Variables "
Private _filename As String = String.Empty ' log file name
Private _folder As String = String.Empty ' folder the log file is
in (always has a trailing \)
Private _logWriter As StreamWriter ' used to write to the
log file
#End Region
#Region " Properties "
Public ReadOnly Property Path() As String
Get
Return _folder & _filename
End Get
End Property
Public ReadOnly Property NewPath() As String
Get
Return _folder & _filename & "_"
End Get
End Property
#End Region
#Region " Methods "
Private Sub OpenLogFile()
Dim sqlLogFileSize As Long = 0
Try
' for debugging purposes
Console.WriteLine("*** Log File Opened : " & Me.Path)
' Create a new streamwriter object
_logWriter = New StreamWriter(Me.Path)
' Make the streamwriter automatically flush changes to the
file to the
' harddisk everytime we use the Write method
_logWriter.AutoFlush = True
' Write a header to the file
_logWriter.WriteLine("test")
Catch ex As Exception
Console.WriteLine("***ERROR : " & ex.Message)
End Try
End Sub
Private Sub CloseLogFile()
' DD Jul 23/04
'
' Closes the log file
Try
_logWriter.Close()
_logWriter = Nothing
GC.Collect()
Console.WriteLine("*** Log File Closed : " & Me.Path)
Try
If IO.File.Exists(Me.NewPath) Then
IO.File.Delete(Me.NewPath)
If Not IO.File.Exists(Me.NewPath) Then
'IO.File.Move(Me.Path, Me.NewPath)
IO.File.Copy(Me.Path, Me.NewPath)
IO.File.Delete(Me.Path)
Console.WriteLine("*** Log File Renamed : " &
Me.NewPath)
Else
Console.WriteLine("*** Log File NOT Renamed.
Another file with the same name already exists and could not be deleted.")
End If
Catch ex As Exception
Console.WriteLine("Error: Log File NOT Renamed: {0}",
ex.Message)
End Try
Catch ex As Exception
Console.WriteLine("Error closing log file: {0}", ex.Message)
End Try
End Sub
#End Region
End Class
--- END ---
Now place a button on Form1 and put the following code in it:
--- START ---
Randomize()
Dim x As StreamWriterTest
x = New StreamWriterTest("test1" & Format(Int(Rnd() * 99999999),
"00000000"))
x = Nothing
GC.Collect()
--- END ---
Looking at the Output Window, you'll see that the IO.File.Delete() raises
the exception. I am at my wits end trying to figure this out.
BTW, I don't think the bug occurs if you try to do the whole thing in, say,
the button's click event. It seems the fact that this code is the Finalize
method of a class has something to do with it.
- Don