该进程无法访问该文件' '因为它正在被另一个进程使用

发布于 2024-12-02 13:28:34 字数 1165 浏览 1 评论 0原文

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

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;
        }
                }

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

○愚か者の日 2024-12-09 13:28:34

您需要处理位图对象尝试这样做。因为这将在位图对象离开使用上下文 { } 后立即对其进行处理

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

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
}
梦回旧景 2024-12-09 13:28:34

首先,我看到

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

CurrentPhoto 我假设您想要保存一些全局变量。

相反,这会引发异常:

 FileSystem.DeleteFile(FilePath);   

导致 FilePath 处的图像文件实际上是 CurrentPhoto。你能做什么。

1)如果使用 CurrentPhoto 在此函数内有任何意义,请在该函数内执行您想要执行的操作,并在处置 CurrentPhoto 对象之后,即使以@Bobby 之类的方式suggest (using block)

2)如果你想顺便拥有的话,可以尝试使用Bitmap的克隆
像这样的方法:

CurrentPhoto = bitmap.Clone();

并且在调用您之后:

bitmap.Dispose();
FileSystem.DeleteFile(FilePath); 

应该有效。

First I see

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

Where CurrentPhoto I presume some global variable that you want hold.

This, instead throws and exception:

 FileSystem.DeleteFile(FilePath);   

Cause image file at FilePath is actually CurrentPhoto. What you can do.

1) If use of CurrentPhoto has any sense inside this function, do what you want to do inside this function, and after dispose CurrentPhoto object, even in a way like @Bobby suggests (using block)

2) If you want to have it by the way, you can try to use Bitmap's Clone
method like this:

CurrentPhoto = bitmap.Clone();

and after call your:

bitmap.Dispose();
FileSystem.DeleteFile(FilePath); 

Should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文