简体   繁体   中英

vb.net delete file used by another process

I Have here a simple code :

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Button 1
    PictureBox1.Image = Image.FromFile("D:\1.jpg")
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'Button 2
    PictureBox1.Image = Nothing
    IO.File.Delete("D:\1.jpg")
End Sub

When i press button 1 to import an image from file then i wanted to delete this image after press button 1 there is an error "The process cannot access the file 'D:\\1.jpg' because it is being used by another process."

that error happen when i press button 2 , any solution ?


(Edited) : Solution Here unable to delete image after opening it in vb.net app

The Image.FromFile method locks the file until the Image object is disposed. Setting the Image property of a PictureBox to Nothing does not dispose the Image object. You need to do that explicitly:

PictureBox1.Image.Dispose()
PictureBox1.Image = Nothing
IO.File.Delete("D:\1.jpg")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM