File in use error
-
This is the code that calls the form where the file is displayed in a picture box.
frmCEA.ContactID = intContactID frmCEA.Text = "Edit a contact" frmCEA.ShowDialog() strAvatarToDelete = frmCEA.AvatarForDeletion frmCEA.Close() frmCEA.Dispose() If strAvatarToDelete.Trim > "" Then My.Computer.FileSystem.DeleteFile(strAvatarToDelete) End If
I have tried on the form, and off the form like this, to delete an avatar but every time I get this message - even if I want to change photos. The process cannot access the file 'C:\Documents and Settings\All Users\Application Data\.....\Avatars\AV000001' because it is being used by another process. Any out there know why this might be??:confused:Are there any other methods in your app using the file? If so, you need to cleanly end those resources so that the file is unlocked. make sure the file is not readonly. Make sure you don't have other apps using the file.
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
Are there any other methods in your app using the file? If so, you need to cleanly end those resources so that the file is unlocked. make sure the file is not readonly. Make sure you don't have other apps using the file.
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison
Thanks for responding. No nothing else is using the file. It just gets placed in the picture box:
Try Me.PictureBox1.BackgroundImage = GetAvatar(ContactID) Me.cmdDeleteImage.Enabled = True Catch ex As Exception Me.cmdDeleteImage.Enabled = False End Try
Where GetAvatar =
Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Image Dim pict As Drawing.Image 'This just collects a file name Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID) Try If strFilename.Trim > "" Then pict = Drawing.Image.FromFile(strFileName) Else pict = My.Resources.nouserpic\_grey End If Catch ex As Exception pict = My.Resources.nouserpic\_grey End Try Return pict End Function
Might it be because it ic alled in from another routine?? :confused:
-
Thanks for responding. No nothing else is using the file. It just gets placed in the picture box:
Try Me.PictureBox1.BackgroundImage = GetAvatar(ContactID) Me.cmdDeleteImage.Enabled = True Catch ex As Exception Me.cmdDeleteImage.Enabled = False End Try
Where GetAvatar =
Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Image Dim pict As Drawing.Image 'This just collects a file name Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID) Try If strFilename.Trim > "" Then pict = Drawing.Image.FromFile(strFileName) Else pict = My.Resources.nouserpic\_grey End If Catch ex As Exception pict = My.Resources.nouserpic\_grey End Try Return pict End Function
Might it be because it ic alled in from another routine?? :confused:
Well, if you rem out the logic in GetAvatar and loading it into the picture box, can you delete the file? :)
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
Thanks for responding. No nothing else is using the file. It just gets placed in the picture box:
Try Me.PictureBox1.BackgroundImage = GetAvatar(ContactID) Me.cmdDeleteImage.Enabled = True Catch ex As Exception Me.cmdDeleteImage.Enabled = False End Try
Where GetAvatar =
Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Image Dim pict As Drawing.Image 'This just collects a file name Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID) Try If strFilename.Trim > "" Then pict = Drawing.Image.FromFile(strFileName) Else pict = My.Resources.nouserpic\_grey End If Catch ex As Exception pict = My.Resources.nouserpic\_grey End Try Return pict End Function
Might it be because it ic alled in from another routine?? :confused:
TheComputerMan wrote:
pict = Drawing.Image.FromFile(strFileName)
This locks the file. You need instead to create a new bitmap. open the file, copy the bitmap, and delete the original. At least, that's how I've got around this bug in GDI+ in the past.
Christian Graus Driven to the arms of OSX by Vista.
-
This is the code that calls the form where the file is displayed in a picture box.
frmCEA.ContactID = intContactID frmCEA.Text = "Edit a contact" frmCEA.ShowDialog() strAvatarToDelete = frmCEA.AvatarForDeletion frmCEA.Close() frmCEA.Dispose() If strAvatarToDelete.Trim > "" Then My.Computer.FileSystem.DeleteFile(strAvatarToDelete) End If
I have tried on the form, and off the form like this, to delete an avatar but every time I get this message - even if I want to change photos. The process cannot access the file 'C:\Documents and Settings\All Users\Application Data\.....\Avatars\AV000001' because it is being used by another process. Any out there know why this might be??:confused:This has been widely documented since the early days of .NET 1.0. The problem is you loaded the image file directly into an Image object in the PictureBox control. The Image object keeps the file locked for it's entire lifetime. The solution is to load the image using a FileStream, then pass the resulting stream to the construcotr of a new Bitmap object, then close the stream, and assign the Bitmap object to the Image property of the PictureBox.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
TheComputerMan wrote:
pict = Drawing.Image.FromFile(strFileName)
This locks the file. You need instead to create a new bitmap. open the file, copy the bitmap, and delete the original. At least, that's how I've got around this bug in GDI+ in the past.
Christian Graus Driven to the arms of OSX by Vista.
I tried that Christian (using pict2 = pict.clone and then disposing pict) but still the same problem. It obviously carries the info accross to the clone.
Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Bitmap Dim pict As Drawing.Bitmap Dim pict2 As Drawing.Bitmap 'This just gets the file name Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID) Try If strFilename.Trim > "" Then pict = Drawing.Bitmap.FromFile(strFilename) pict2 = pict.Clone() pict.Dispose() Else pict2 = My.Resources.nouserpic\_grey End If Catch ex As Exception pict2 = My.Resources.nouserpic\_grey End Try Return pict2 End Function
Looks like I might have to try storing the pic in the database, but I did not want to do that as it is only an SQL Server 2005 Compact so space limited. Thanks anyway! (If you can think of any other way I would like to hear from you)
-
Well, if you rem out the logic in GetAvatar and loading it into the picture box, can you delete the file? :)
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison
Thanks for the suggestion Jon_Boy but unfortunately that would impact my code. I will give it a go if all else fails however.
-
This has been widely documented since the early days of .NET 1.0. The problem is you loaded the image file directly into an Image object in the PictureBox control. The Image object keeps the file locked for it's entire lifetime. The solution is to load the image using a FileStream, then pass the resulting stream to the construcotr of a new Bitmap object, then close the stream, and assign the Bitmap object to the Image property of the PictureBox.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Thanks Dave. I think I know how to do that so I will give that a go.
-
This has been widely documented since the early days of .NET 1.0. The problem is you loaded the image file directly into an Image object in the PictureBox control. The Image object keeps the file locked for it's entire lifetime. The solution is to load the image using a FileStream, then pass the resulting stream to the construcotr of a new Bitmap object, then close the stream, and assign the Bitmap object to the Image property of the PictureBox.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Thanks a million Dave. It worked a treat! :) :) :) No need to reply to this. Just posting the code in case it helps someone else.
Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Bitmap Dim pict As Drawing.Bitmap 'This just gets the file name Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID) Try If strFilename.Trim > "" Then Dim fs As New System.IO.FileStream(strFilename, IO.FileMode.Open, IO.FileAccess.Read) pict = Drawing.Bitmap.FromStream(fs) fs.Close() Else pict = My.Resources.nouserpic\_grey End If Catch ex As Exception pict = My.Resources.nouserpic\_grey End Try Return pict End Function
Code above corrected: :) Thanks Fabio
modified on Saturday, January 3, 2009 10:20 AM
-
I tried that Christian (using pict2 = pict.clone and then disposing pict) but still the same problem. It obviously carries the info accross to the clone.
Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Bitmap Dim pict As Drawing.Bitmap Dim pict2 As Drawing.Bitmap 'This just gets the file name Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID) Try If strFilename.Trim > "" Then pict = Drawing.Bitmap.FromFile(strFilename) pict2 = pict.Clone() pict.Dispose() Else pict2 = My.Resources.nouserpic\_grey End If Catch ex As Exception pict2 = My.Resources.nouserpic\_grey End Try Return pict2 End Function
Looks like I might have to try storing the pic in the database, but I did not want to do that as it is only an SQL Server 2005 Compact so space limited. Thanks anyway! (If you can think of any other way I would like to hear from you)
TheComputerMan wrote:
pict2 = pict.Clone()
Christian is right, but you did not take his advise.
TheComputerMan wrote:
Bitmap.FromFile(strFilename)
This is what keeps your file locked. Hence make and use a copy of the IMAGE and dispose of the original right away.
TheComputerMan wrote:
any other way
No need. The one way is fine if executed properly. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Love, happiness and fewer bugs for 2009!
-
Thanks for the suggestion Jon_Boy but unfortunately that would impact my code. I will give it a go if all else fails however.
Wasn't suggesting that as a perm. fix - just as a way for you now and in the future easily determine if and where your code was locking a file.
Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
Thanks a million Dave. It worked a treat! :) :) :) No need to reply to this. Just posting the code in case it helps someone else.
Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Bitmap Dim pict As Drawing.Bitmap 'This just gets the file name Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID) Try If strFilename.Trim > "" Then Dim fs As New System.IO.FileStream(strFilename, IO.FileMode.Open, IO.FileAccess.Read) pict = Drawing.Bitmap.FromStream(fs) fs.Close() Else pict = My.Resources.nouserpic\_grey End If Catch ex As Exception pict = My.Resources.nouserpic\_grey End Try Return pict End Function
Code above corrected: :) Thanks Fabio
modified on Saturday, January 3, 2009 10:20 AM
Why do you need picbyteArray variable?! You're not using it...
-
Why do you need picbyteArray variable?! You're not using it...
Hi Fabio, Thanks for that. I had pulled the code from another program where I am using it to store the picture in SQL Server and forgot to take that bit out! Duh!! :rolleyes: The corrected code block is:
Friend Function GetAvatar(ByVal ContactID As Integer) As Drawing.Bitmap Dim pict As Drawing.Bitmap 'This just gets the file name Dim strFilename As String = cDBHandler.ExtractContactImage(ContactID) Try If strFilename.Trim > "" Then Dim fs As New System.IO.FileStream(strFilename, IO.FileMode.Open, IO.FileAccess.Read) pict = Drawing.Bitmap.FromStream(fs) fs.Close() Else pict = My.Resources.nouserpic\_grey End If Catch ex As Exception pict = My.Resources.nouserpic\_grey End Try Return pict End Function
Sorry if that caused any confusion to anyone.