vb.net 该进程无法访问文件“x”,因为它正在被另一个进程使用,同时应用程序删除保存的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17213605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The process cannot access the file 'x' because it is being used by another process while deleting saved image by application
提问by Pratik
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.
ma 初学者 n 设计一个将图像保存在硬盘上并将其名称保存在 sql 表中的测试应用程序。我可以保存、浏览记录但无法删除图像。
it gives me error The process cannot access the file 'x' because it is being used by another process while deleting image
它给了我错误进程无法访问文件“x”,因为它正在被另一个进程在删除图像时使用
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
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 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.
我尝试处理图像和图片框并在图片框中加载另一个图像也没有使用任何文件对象来打开文件,除了 Image 类的 From file 方法。
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:
处置没有用。它没有处理图像,图像仍然被使用,但是当我尝试手动删除该特定图像时,我向我显示了该文件已被“ vshost.exe ”使用的错误,该文件是我的应用程序本身。因此,我按照 James 的说明使用 File Stream 在图像框中加载图片,但是当我尝试使用以下代码删除任何图像时,它仍然给我错误:
File.Delete("D:\EmpImages\" & iiImageNo & ".jpg")
File.Delete("D:\EmpImages\" & iiImageNo & ".jpg")
File.Delete("D:\EmpImages\" & iiImageNo & ".jpg")
File.Delete("D:\EmpImages\" & iiImageNo & ".jpg")
so I tried this
所以我试过这个
FileSystem.Kill("D:\EmpImages\" & iiImageNo & ".jpeg")
FileSystem.Kill("D:\EmpImages\" & iiImageNo & ".jpeg")
and it worked. thank you james and everyone who gave there precious time for me
它奏效了。谢谢詹姆斯和所有为我付出宝贵时间的人
回答by Raymond Chen
This is explained in the documentation for Image.FromFile:
The file remains locked until the Image is disposed.
文件保持锁定状态,直到图像被释放。
回答by James
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 Streame.g.
当您尝试从磁盘中删除图像时,图片框可能仍未释放图像,更可靠的方法是从Stream例如加载图像
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.
这似乎是一个常见问题。
回答by TOM
Dim xx as Image
Using str As Stream = File.OpenRead(Fileloc)
xx = Image.FromStream(str)
End Using
picturebox.Image = xx

