文件正在被另一个进程使用... [英] File is being used by another process...

查看:86
本文介绍了文件正在被另一个进程使用...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在表格加载中

On Form load

Dim imagepath As String = "C:\Data\temp.gif"

If File.Exists(imagepath) Then
  Dim opimg As Image = Image.FromFile(imagepath)
  PictureBox1.Image = opimg
End If



稍后检查更新/更改图像



later check for update/change image

PictureBox1.Image = webDownloadImage(image_url, imagepath)

Public Function webDownloadImage(ByVal Url As String, Optional ByVal location As String = "C:\") As Image
    Dim webClient As New System.Net.WebClient
    Dim bytes() As Byte = webClient.DownloadData(Url)

    My.Computer.FileSystem.WriteAllBytes(location, bytes, False)
    Return New System.Drawing.Bitmap(Stream)
  End Function



此代码在
处显示错误 My.Computer.FileSystem.WriteAllBytes(位置,字节,False)

该文件无法访问该文件,因为它已被另一个进程使用"


请帮助



this code shows error at
My.Computer.FileSystem.WriteAllBytes(location, bytes, False)

"the file cannot access the file because it used by another process"


Please help

推荐答案

确保文件未在绘画或任何其他图像编辑器中打开.
Make sure the file is not open in paint or any other image editor.


尝试处置对象.

try disposing the bytes object.

PictureBox1.Image = webDownloadImage(image_url, imagepath)

Public Function webDownloadImage(ByVal Url As String, Optional ByVal location As String = "C:\") As Image
    Dim webClient As New System.Net.WebClient
    Dim bytes As Byte = webClient.DownloadData(Url)

    bytes.Dispose()

    My.Computer.FileSystem.WriteAllBytes(location, bytes, False)
    Return New System.Drawing.Bitmap(Stream)
  End Function


该过程可能是您的自己的应用程序,如果您的代码已经打开了文件,但是在将文件传递给此方法之前从未关闭过它.

此外,Vista和7台计算机上的C:\对用户是只读的.如果您以普通用户身份运行代码,则此写入操作将失败.
That process could be your own application if your code already has the file opened, but never closed it before passing the file to this method.

Also, C:\ on Vista and 7 machines is read-only to Users. If you''re code is running as a normal user, this write will fail.


这篇关于文件正在被另一个进程使用...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆