Pratik
Pratik

Reputation: 23

The process cannot access the file 'x' because it is being used by another process while deleting saved image by application

m a beginner n designing a test application of saving image on hard disk and its name in sql table. I am able to save, navigate through the records but not able to delete the image.

it gives me error The process cannot access the file 'x' because it is being used by another process while deleting image

the code is as follows:

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click isProcName = "btnDelete_Click" OBJ = New clsImageStoring

    Try
        Dim result As DialogResult = MessageBox.Show(Me, "Do you really want to delete this Record?", "Query", vbYesNo, vbQuestion)
        If result = Windows.Forms.DialogResult.Yes Then
            iiId = DGV.Rows(iiRowno).Cells(0).Value
            iiImageNo = DGV.Rows(iiRowno).Cells(1).Value
            Dim liTempImageNo As Int64 = 0
            If OBJ.Delete(iiId) Then
                Fillgrid()
                liTempImageNo = DGV.Rows(0).Cells(1).Value
                picEmp.Image.Dispose()

                picEmp.Image = Image.FromFile("D:\EmpImages\" & liTempImageNo & ".jpg")
                'File.Delete("D:\EmpImages\" & iiImageNo & ".jpg")
                FileIO.FileSystem.DeleteFile("D:\EmpImages\" & iiImageNo & ".jpg")
                MessageBox.Show(Me, "Record Deleted Successfully", "Information", vbOKOnly, vbInformation)
            End If
        End If

    Catch ex As Exception
        clsLog.WriteException(ex, isModuleName, isProcName)
    End Try
End Sub

I tried disposing image and from picture box and loading another image in picture box also m not using any file object for opening the file except the Image class's From file method.

any help will be appreciated Thankyou


dispose didn't work. It is not disposing the image the image was still been used but when I tried to manually delete that particular image I showed me error that the file is been used by " vshost.exe " which is my application itself. So I used File Stream to load picture in image box as been told by James but it still gave me error when I tried deleting any image with following code:

File.Delete("D:\EmpImages\" & iiImageNo & ".jpg")
File.Delete("D:\EmpImages\" & iiImageNo & ".jpg")

so I tried this

FileSystem.Kill("D:\EmpImages\" & iiImageNo & ".jpeg")

and it worked. thank you james and everyone who gave there precious time for me

Upvotes: 2

Views: 14353

Answers (3)

TOM
TOM

Reputation: 881

Dim xx as Image
Using str As Stream = File.OpenRead(Fileloc)
xx = Image.FromStream(str)
End Using
picturebox.Image = xx

Upvotes: 2

James
James

Reputation: 82136

The picture box probably still hasn't released the image by the time you attempt to delete it from disk, a more reliable approach would be to load the image from a Stream e.g.

Using fs As New System.IO.FileStream("file path", IO.FileMode.Open, IO.FileAccess.Read)
    PictureBox1.Image = System.Drawing.Image.FromStream(fs)
End Using

This would prevent any sort of locking on the file.

It does appear to be a common problem.

Upvotes: 5

Raymond Chen
Raymond Chen

Reputation: 45172

This is explained in the documentation for Image.FromFile:

The file remains locked until the Image is disposed.

Upvotes: 6

Related Questions