1

My code goes like this:

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

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.

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Jill
  • 87
  • 3
  • 14

2 Answers2

1

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.

Here are a couple of links:

The dispose of an OpenFileDialog in C#?

C#: Dispose() a Bitmap object after call Bitmap.save()?

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.

Community
  • 1
  • 1
RenniePet
  • 11,420
  • 7
  • 80
  • 106
0

You need to close the file after you're done with. Use the .Close() property to close the file.

Once you open a file to read it/use it, it opens a background process. In order for that process to end, you need to also programatically close it using the .Close()

So your code needs to have a yourFile.Close() at the bottom of your code.

mwilson
  • 12,295
  • 7
  • 55
  • 95
  • *it opens a background process* since when do files spin a process? Do you mean open a *file handle*? – Yuval Itzchakov Aug 24 '14 at 04:34
  • Sorry, maybe 'background process' isn't the correct terminology. But after you run your program, take a look at your task list. Although you don't visually see the file opened up, you can see that it is in fact still open and present in your task list. In order to end the task when your program is complete, you need to Close it. – mwilson Aug 24 '14 at 22:53
  • Think of it like this: When you open up a word document, for example, you launch the program via the start menu or icon. When you're done with that document, you then close out of the program by doing File > Close or by pressing the X. It's the same concept here where you launched the document programatically but never closed it. So when you run your program again or try and do something that document you opened, you'll get an error saying that it's being used another process (because it was never closed). – mwilson Aug 24 '14 at 23:08
  • And what happens when i open a file in memory and write to it, such as a text file. Which process opens then? None, only a file handle is registered with the OS. – Yuval Itzchakov Aug 24 '14 at 23:23
  • "And what happens when i open a fi" Do you mean File? – mwilson Aug 24 '14 at 23:25