.net 进程无法访问文件xxxxxx,因为另一进程正在使用该文件

.net 进程无法访问文件xxxxxx,因为另一进程正在使用该文件,.net,vb.net,file,delete-file,.net,Vb.net,File,Delete File,我正在使用VS2010和VS2012以及.Net4编写一个WinForm应用程序 在应用程序中,我将单个图像加载到PictureBox中,然后删除用户不希望保留的图像 每次尝试删除这些“不需要的”图像时,我都会收到上面的错误消息: “进程无法访问文件xxxxxx,因为另一个进程正在使用该文件” 以下代码用于将图像加载到动态创建的picturebox中,并删除不需要的图像 选项1: 加载图像: picBox.Image = Image.FromFile(imgInfo.FullName).GetT

我正在使用VS2010和VS2012以及.Net4编写一个WinForm应用程序

在应用程序中,我将单个图像加载到PictureBox中,然后删除用户不希望保留的图像

每次尝试删除这些“不需要的”图像时,我都会收到上面的错误消息:

“进程无法访问文件xxxxxx,因为另一个进程正在使用该文件”

以下代码用于将图像加载到动态创建的picturebox中,并删除不需要的图像

选项1:

加载图像:

picBox.Image = Image.FromFile(imgInfo.FullName).GetThumbnailImage(128, 128, Nothing, Nothing)
删除图像:

For Each imgFile As String In Directory.GetFiles(imgSharedFolder)
   File.Delete(imgFile)
Next imgFile
选项2:

由于将图像文件加载到picturebox会“锁定”图像文件,因此我尝试了以下方法:将图像文件读入文件流,然后在将图像加载到picturebox后关闭文件流

负载:

删除:

Dim picList As String() = Directory.GetFiles(imgSharedFolder, "*.jpg")
For Each f As String In picList
   File.Delete(f)
Next f
我收到了相同的错误消息

选项3:

经过更多的搜索、阅读和尝试,我发现了以下建议:创建一个图像对象,然后在图像加载到picturebox后处理该图像对象:

负载:

不幸的是,我刚刚收到了相同的错误消息


这个问题还有其他可能的解决方案吗?我可能忽略了这一点。

这是我在上面发布的问题的解决方案

虽然我正在将图像文件读入PictureBox,但我忽略了提醒自己,在将图像保存到数据库时,我会执行一个非常类似的过程

只有在我开始分解在将图像读入Picbox和将它们写入DB之间发生的一切之后,我才设法找到解决问题的正确方法

以下是将图像文件读入PictureBox的过程:

    For Each imgFile As String In Directory.GetFiles(fbdGetImagesOnDisk.SelectedPath)
       Try
          Dim fs As System.IO.FileStream
          imgInfo = New FileInfo(imgFile)
          If myext.Contains(LCase(imgInfo.Extension)) Then
             'Create dynamic picturebox
             picBox = New PictureBox()

             'Specify a valid picture file path
             fs = New System.IO.FileStream(imgInfo.FullName, IO.FileMode.Open, IO.FileAccess.Read)
             picBox.Image = System.Drawing.Image.FromStream(fs).GetThumbnailImage(128, 128, Nothing, Nothing)
             fs.Close()
   Catch ex as Exception
      msgbox(ex.message)
   End Catch
   Next imgFile
下面的代码是在删除磁盘上的映像文件之前将映像从磁盘写入DB重要提示:请注意fs.Close()的位置,因为如果放置不正确,它将生成GDI+一般错误,并且无法删除最后一个图像

Try
   conn.Open()
   'Create a FileStream for the image file on disk. This will allow us to Close the stream and release the file lock
   Dim fs As System.IO.FileStream
   'Specify a valid picture file path
   fs = New System.IO.FileStream(imgPath & "\" & imgName, IO.FileMode.Open, IO.FileAccess.Read)
   'Create an Image object to store the image.
   Dim img As Image = Image.FromStream(fs)
   'Create a Thumbnail object to store the Thumbnail of the image (128,128)
   Dim imgThumbnail As Image = Image.FromStream(fs).GetThumbnailImage(128, 128, Nothing, Nothing)
   'Create an empty stream in memory for the Image.
   Dim imgStream As New IO.MemoryStream
   'Create an empty stream in memory for the Thumbnail of the image
   Dim tmbStream As New IO.MemoryStream
   'Fill the stream with the binary data from the Image.
   img.Save(imgStream, Imaging.ImageFormat.Jpeg)
   'Fill the empty stream with the data of the Thumbnail
   imgThumbnail.Save(tmbStream, Imaging.ImageFormat.Jpeg)
   'Store Image memory stream as array
   Dim arrImage() As Byte = imgStream.GetBuffer()
   'Store Thumbnail memory stream as array
   Dim arrThumbnail() As Byte = tmbStream.GetBuffer()
   'Close the streams
   imgStream.Close()
   tmbStream.Close()
   'Close the File Stream
   **fs.Close()**

   With cmd
      .CommandText = SQLstr
      .Connection = conn
      .Parameters.Clear()
      .Parameters.AddWithValue("@imgName", imgName)
      .Parameters.AddWithValue("@imgImage", arrImage)
      .Parameters.AddWithValue("@imgThumbnail", arrThumbnail)
      .Parameters.AddWithValue("@patID", CInt(patID))
      .Parameters.AddWithValue("@conID", CInt(conID))
      .ExecuteNonQuery()
   End With

   Return True

Catch ex As Exception
   MsgBox(ex.Message)
   Return False
End Try
conn.Close() 'Close DB connection
在将映像写入数据库后,我们现在可以开始删除磁盘上的映像文件,而不会收到关于文件被锁定的任何错误消息。 在将映像文件写入数据库后,从磁盘中删除映像文件的代码如下:

Try
   Dim picList As String() = Directory.GetFiles(imgSharedFolder, "*.jpg")
   For Each f As String In picList
      File.Delete(f)
   Next f
Catch ex As Exception
   MsgBox(ex.Message)
End Try

感谢那些回复的人,因为他们再次确认了流程背后的大量信息,以及为什么应该以特定的方式进行。如果没有您的帮助和指导,我可能会被困更长的时间。

@PorkChop:再看一次,FS.Close位于选项2中加载函数的第3行。可能是@pmColtrane:的副本,现在正在查看您的链接。谢谢,因为它是非常全面和详细的。从表面上看,它几乎像一个复制品。。。(如果建议的解决方案有效,则仅复制xD)
Try
   conn.Open()
   'Create a FileStream for the image file on disk. This will allow us to Close the stream and release the file lock
   Dim fs As System.IO.FileStream
   'Specify a valid picture file path
   fs = New System.IO.FileStream(imgPath & "\" & imgName, IO.FileMode.Open, IO.FileAccess.Read)
   'Create an Image object to store the image.
   Dim img As Image = Image.FromStream(fs)
   'Create a Thumbnail object to store the Thumbnail of the image (128,128)
   Dim imgThumbnail As Image = Image.FromStream(fs).GetThumbnailImage(128, 128, Nothing, Nothing)
   'Create an empty stream in memory for the Image.
   Dim imgStream As New IO.MemoryStream
   'Create an empty stream in memory for the Thumbnail of the image
   Dim tmbStream As New IO.MemoryStream
   'Fill the stream with the binary data from the Image.
   img.Save(imgStream, Imaging.ImageFormat.Jpeg)
   'Fill the empty stream with the data of the Thumbnail
   imgThumbnail.Save(tmbStream, Imaging.ImageFormat.Jpeg)
   'Store Image memory stream as array
   Dim arrImage() As Byte = imgStream.GetBuffer()
   'Store Thumbnail memory stream as array
   Dim arrThumbnail() As Byte = tmbStream.GetBuffer()
   'Close the streams
   imgStream.Close()
   tmbStream.Close()
   'Close the File Stream
   **fs.Close()**

   With cmd
      .CommandText = SQLstr
      .Connection = conn
      .Parameters.Clear()
      .Parameters.AddWithValue("@imgName", imgName)
      .Parameters.AddWithValue("@imgImage", arrImage)
      .Parameters.AddWithValue("@imgThumbnail", arrThumbnail)
      .Parameters.AddWithValue("@patID", CInt(patID))
      .Parameters.AddWithValue("@conID", CInt(conID))
      .ExecuteNonQuery()
   End With

   Return True

Catch ex As Exception
   MsgBox(ex.Message)
   Return False
End Try
conn.Close() 'Close DB connection
Try
   Dim picList As String() = Directory.GetFiles(imgSharedFolder, "*.jpg")
   For Each f As String In picList
      File.Delete(f)
   Next f
Catch ex As Exception
   MsgBox(ex.Message)
End Try