该进程无法访问该文件,因为它正在被另一个进程使用 [英] The process cannot access the file ' ' because it is being used by another process

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

问题描述

我正尝试删除使用文件对话框上传的图像文件的本地副本(在计算机上)。它引发进程无法访问文件C:\ Documents and Settings\username\我的文档\我的图片\1220.bmp,因为它正在被另一个进程使用。

  private void _btnImportPhoto_Click(object sender,RoutedEventArgs e)
{
//用户单击导入/更改照片,打开文件对话框以浏览photo
System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
fileDialog.Multiselect = false;
fileDialog.Filter = ResourceFile.PhotoFileTypes;

if(fileDialog.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
FilePath = fileDialog.FileName;
FilePathCopy = fileDialog.FileName;
字符串safeFilePath = fileDialog.SafeFileName;

位图位图=新的位图(FilePath);
CurrentPhoto = bitmap;

位图bitmap1 =新的位图(FilePathCopy); //选择删除本地副本时保存副本
m_PhotoCopy = bitmap1;


FileSystem.DeleteFile(FilePath);
}
_btnSave.IsEnabled = _btnCancel.IsEnabled = true;




$ b

请让我知道如何解决此问题。
Thanks。

解决方案

您需要处理位图对象。因为这会在它离开使用的上下文的时候立即丢弃位图对象。(使用位图bitmap1 = new Bitmap(bitmap bitmap1 = new Bitmap() FilePathCopy))
{
//在这里做所有位图的东西
}


I am trying to delete local copy(on the computer) of an image file once uploaded using file dialog. It throws The process cannot access the file 'C:\Documents and Settings\username\My Documents\My Pictures\1220.bmp' because it is being used by another process.

private void _btnImportPhoto_Click(object sender, RoutedEventArgs e)
    {
        //user clicked import/change photo, open file dialog to browse for photo
        System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
        fileDialog.Multiselect = false;
        fileDialog.Filter = ResourceFile.PhotoFileTypes;

        if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            FilePath = fileDialog.FileName;
            FilePathCopy = fileDialog.FileName;
            string safeFilePath = fileDialog.SafeFileName;

            Bitmap bitmap = new Bitmap(FilePath);
            CurrentPhoto = bitmap;

            Bitmap bitmap1 = new Bitmap(FilePathCopy); //A copy to save when if delete local copy is chosen
            m_PhotoCopy = bitmap1;


             FileSystem.DeleteFile(FilePath);                   
            }
            _btnSave.IsEnabled = _btnCancel.IsEnabled = true;
        }
                }

Please let me know how to work around this issue. Thanks.

解决方案

you need to dispose of the bitmap object try doing this. As this will dispose of the bitmap object as soon as it leaves the using context { }

using (Bitmap bitmap1 = new Bitmap(FilePathCopy))
{
  //do all bitmap stuff in here
}

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

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