File.Copy“进程无法访问文件< filename>因为它正被另一个进程使用 [英] File.Copy "The process cannot access the file <filename> because it is being used by another process

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

问题描述

我的代码是这样的:

using (var openFileDialogForImgUser = new OpenFileDialog())
{
    string location = null;
    string fileName = null;
    string sourceFile = null;
    string destFile = null;
    string targetPath = @"..\..\Images";
    openFileDialogForImgUser.Filter = "Image Files (*.jpg, *.png, *.gif, *.bmp)|*.jpg; *.png; *.gif; *.bmp|All Files (*.*)|*.*"; // filtering only picture file types
    openFileDialogForImgUser.InitialDirectory = @"D:\My Pictures";
    var openFileResult = openFileDialogForImgUser.ShowDialog(); // show the file open dialog box
    if (openFileResult == DialogResult.OK)
    {
        using (var formSaveImg = new FormSave())
        {
            var saveResult = formSaveImg.ShowDialog();
            if (saveResult == DialogResult.Yes)
            {
                imgUser.Image = new Bitmap(openFileDialogForImgUser.FileName); //showing the image opened in the picturebox
                fileName = openFileDialogForImgUser.FileName;
                location = Path.GetDirectoryName(fileName);

                sourceFile = System.IO.Path.Combine(location, fileName);
                destFile = System.IO.Path.Combine(targetPath, fileName);

                if (!System.IO.Directory.Exists(targetPath))
                {
                    System.IO.Directory.CreateDirectory(targetPath);
                }

                System.IO.File.Copy(sourceFile, destFile, true); /* error occurs at this line */

            }
            else
                openFileDialogForImgUser.Dispose();
        }
    }
}

我想要的是什么我提示用户在OpenFileDialog中选择一个图像,并将该图片复制到另一个目录(具体是targetPath)。一切似乎工作正常,除了那一行: System.IO.File.Copy(sourceFile,destFile,true);

What I'm trying to do is that I am prompting the user to select an image in an OpenFileDialog, and copy that picture to a different directory (targetPath, specifically). Everything seems to work fine except for that one line: System.IO.File.Copy(sourceFile, destFile, true);.

它表示该文件仅供另一个进程使用。对于我选择的任何图像都会发生这种情况。

It says that the file is used exclusively used by another process. And that happens for any image I select.

我正试图查看别人对这个问题的解决方案---我必须终止(或等待) )对于使用我的文件的进程。如果是这样,我该怎么做?或者,如果这不是答案,我该怎么办?请注意,这是我在C#解决方案中处理图像文件的唯一代码。

I'm trying to look at others' solution(s) to this problem --- I have to terminate (or wait) for the process that's using my file. If so, how do I do that? Or, if that's not the answer to this, what should I do? Note that this is the only piece of code that deals with image files in my C# solution.

推荐答案

我认为您的问题不涉及正确处理Bitmap对象。如果您创建一个位图,使其从文件中读取而您不进行处理,则该文件将保持打开并锁定。

I think your problem involves not disposing of the Bitmap object properly. If you create a Bitmap such that it reads from a file and you don't dispose it the file remains open and locked.

以下是几个链接:

在C#中处理OpenFileDialog?

C#:调用Bitmap.save()之后Dispose()一个Bitmap对象?

正如我在本答复中指出的那样 https://stackoverflow.com / a / 18172367/253938 我使用Bitmap和磁盘文件的经验是,最好永远不要让Bitmap打开文件。相反,将文件读入字节数组并使用ImageConverter将其转换为图像。

As I indicate in this answer https://stackoverflow.com/a/18172367/253938 my experience with using Bitmap and on-disk files is that it's best to never let Bitmap open the file. Instead, read the file into a byte array and use ImageConverter to convert it into an Image.

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

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