I made a simple program(for my VB.Net practice). The program loads an image file to a picturebox upon clicking a button. after loading the image file i have another button to delete the loaded image file from the directory. But I have a problem deleting the image file used by the picturebox even if I set the "PictureBox1.Image = Nothing" before deletion takes place. The error says, "The process cannot access the file because it is being used by another process". What must be the problem with this? can somebody give me an Idea how will i deal with this problem?
Here's my code on delete process:
Expand|Select|Wrap|Line Numbers
- Private Sub RemoveFromDirectory(ByVal strFile As String)
- Dim strDirectory As String
- strDirectory = Application.StartupPath & "\Images"
- If File.Exists(strDirectory & "\" & strFile & ".jpg") = True Then
- PictureBox1.Image = Nothing
- File.Delete(strDirectory & "\" & strFile & ".jpg")
- End If
- End Sub