cannot delete file because it is in use by another process

Jun 14 2007 5:59 AM
situation:
I'v created a form with loads server usercontrols. In those usercontrols are all images in a certain folder.
Problem:
I want the user to be able to select one and press delete so that the file is deleted from the hard drive.
I know the path from the file (= property in usercontrol) but when I use
system.io.file.delete(path) I get the folowing message:
"The process cannot access the file 'C:\Documents and Settings\Tom\Mijn documenten\Mijn afbeeldingen\Kopie van Winter.jpg' because it is being used by another process."

After some research I found out that when I use picterbox1.image = Drawing.Image.FromFile(path) vb apperently keeps a link to the file and keeps this file locked.

I thought about declaring a new bitmap put the image in that one and use the internal bitmap. this works fine but the problem I have then is that my tif files only display 1 page and since the program is going to be used in an envoriment where the have alot of tif files (or file like it (more than 1 page in the image file)) this is not really an option.

my code:

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If MessageBox.Show("Bent u zeker dat u deze file DEFINITIEF wilt verwijderen?", "Bevestiging", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = Windows.Forms.DialogResult.OK Then
For Each c As Control In flp.Controls
If c.GetType.Equals(GetType(Preview)) Then
Dim pr As Preview = c
If pr.checked Then
pr.Image = Nothing 'sets the usercontrol image to nothing
flp.Controls.Remove(c) 'removes the usercontrol from the flowlayoutpanel
File.Delete(pr.Path)
End If
End If
Next
End If
End Sub



Anyone know how I can onluck the file so that I can delete it?

Answers (1)