[Resolved]Files modified by VB are in a locked state
I have some code that does a Find & Replace in some text files, and closes the file when complete. However, when I try to use the Kill command in VB to cleanup these files (they are temporary files) when the program is closed, I get errors that the file is in use by another app or process. Is there a way I can force these files to close first? This also happens when I scan a document using the EZTWAIN.dll Scan to native function. Everything goes fine, but if I try to scan again to overwrite the file, the file is in use. This is happening on Windows Vista SP1. Maybe I am missing an important step somewhere, but I don't understand why my app isn't releasing the files. Thanks for any help on this issue.
-jeremy
Re: Files modified by VB are in a locked state
Could you show us the code you're using to open, edit, and close the files?
Re: Files modified by VB are in a locked state
are you working with these files as fileinfo objects? or are you opening a filestream and writing to a file?
I've run into tons of issues with fileinfo and IO locking. On a polling/ftp application I did a while back i found out that releasing references to a fileinfo object doesn't signal for the locks to release too. In my experiences I had to do a Using Filestream / End Using and then attempt to kill the file.
Re: Files modified by VB are in a locked state
The issue appears to be with an image I am loading into a Picturebox control with and OpenFileDialog control. It appears the handle to the file is remaining open after image has been loading into the picture box. Any idea how to make VB close the file handle? My code to load the image to the picturebox with a command button is below:
Code:
Dim OpenPic As New OpenFileDialog
Dim strClientPicPath As String
With OpenPic
.CheckFileExists = True
.ShowReadOnly = False
.Filter = "JPEG (*.jpg) |*.jpg"
.FilterIndex = 2
If .ShowDialog = DialogResult.OK Then
' Load the specified file into a PictureBox control.
strClientPicPath = OpenPic.FileName
frmCropImage.pbImageCrop.Image = Image.FromFile(.FileName)
End If
End With
Me.Enabled = False
frmCropImage.Show()
Thanks for any suggestions.
-Jeremy
Re: Files modified by VB are in a locked state
Image.FromFile does not close the filestream created when opening the image. Use the PictureBox' Load method instead.
Re: Files modified by VB are in a locked state
Re: Files modified by VB are in a locked state
Which method will allow me to modify the least amount of my existing code? I need to use the OpenFileDialog control for the user to browse to the image, but I need to load it into the picturebox without it remaining locked. All examples I found using the Picturebox.load method referred to loading a URL image.
Also, I can't simply use:
Code:
frmCropImage.pbImageCrop.Image = Image.FromStream(.FileName)
because the (.Filename) is a string, not system.io.
Thanks for suggestions.
-Jeremy
Re: Files modified by VB are in a locked state
Have you tried the Load method?
Re: Files modified by VB are in a locked state
So I need to store the path of the openfiledialog selected file as a bitmap and then paint the bitmap into the picturebox? Is that the easiest method?
thanks
Re: Files modified by VB are in a locked state
No, just use the Load method:
Code:
frmCropImage.pbImageCrop.Load(.FileName)
Re: Files modified by VB are in a locked state
Oh, Ok. That is MUCH easier. Thanks for the help.