File Access problem

sj1187534

Well-known member
Joined
Jun 10, 2003
Messages
108
Location
Dallas, Houston, etc.etc.
Dim dt As DateTime = DateTime.Now()
Dim permlogfilename As String = "Gisb_Log_Out_" & dt.Year & dt.Month & dt.Day & dt.Hour & dt.Minute & dt.Second & ".txt"
Dim permlogfilepath As String = Me.logDir & "OUTLOG\" & permlogfilename
templogfileinfo.CopyTo(permlogfilepath)
templogfileinfo.Delete()

When it is attemtping to copy the file at the .CopyTo command, it is saying that the file represented by "permlogfilepath" is being used by another process.

Any ideas?

SJ
 
Last edited by a moderator:
It looks like this line

permlogwriter.Write(templogfileinfo.OpenRead)

opens templogfileinfo and you never close it, you close permlogwriter.

try

permlogwriter.Write(templogfileinfo.OpenRead)
permlogwriter.flush()
templogfileinfo.close()
permlogwriter.close()
 
Yes...I did. The code that I removed was redundant. Even the code I have right now is giving me the same error. Seems strange to me. That is the only instance of the templogfile I am using.

By the way, a fileinfo class does not have a Close() method.

SJ

talahaski said:
looks like you just edited your post and removed all the important code.
 
Sorry, hard to keep track of what classes have close methods and which ones do not.

You might want to try this to figure out exactly when the file gets locked. Run in debug mode and step through line by line, between stepping through each line of code, open a window explorer, find the source and dest files, try to rename these files. If the rename works, nothing is holding the file open, rename it back to the original name, and run the next step and repeat. Eventually you will find that the rename give a error, at this point go back to the previous line of code you ran and that is the problem.
 
Hey sj,

Try to implement youre own dispose and finalize methods on the class. Make the class inherit idisposable and then implement a dispose method that closes any stream.

Even though there is no close method on a FileInfo object, sometime it still gets stored in the heap until the garbage collector picks it up.

It might be as simple as invoking the garbage collector automatically in the dispose method:

Public Overloads Sub Dispose() Implements IDisposable.Dispose
If DisposeCalled Then
Return
End If
call your stream closures here
GC.SuppressFinalize(Me)
DisposeCalled = True

End Sub

hope this helps.

inzo
 
Back
Top