1

I have written a winform application in visual studio 2010.In one of the forms the user can browse the local system and select an image, so the application will copy that image(with File.Copy() method) to its folder. the problem is when the user wants to delete that image(File.Delete() method),I receive an error like this :

cannot delete this file because it is used by another process.

I do not know what this error says because i do not use the image files in other processes.

Steve
  • 213,761
  • 22
  • 232
  • 286
M_Mogharrabi
  • 1,369
  • 5
  • 29
  • 57
  • Could you please post the code that you are using? – matthewr May 05 '12 at 05:38
  • You can use [process explorer](http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx) to find the process that has the handle. My guess is that you forgot `.Close()` somewhere but this will confirm it. – Conrad Frix May 05 '12 at 05:40
  • 1
    Download Process Monitor: it can show you exactly what process(s) are using the file: http://technet.microsoft.com/en-us/sysinternals/bb896645 I'm betting you inadvertantly opened it with your own winform app ;) – paulsm4 May 05 '12 at 05:41
  • 1
    I am showing the image in a pictureBox control in the form.do you think it can be the problem??!! – M_Mogharrabi May 05 '12 at 06:55

3 Answers3

4

cannot delete this file because it is used by another process.

The message isn't terribly helpful to programmers because when it happens when you develop code, that other process is almost always your process.

This is very likely to occur with image files, creating a Image or Bitmap object from an image file puts a lock on the file. The lock is created because GDI+ creates a memory-mapped view on the file content, a strong optimization that keeps the bitmap data out of the paging file. Matters a great deal on large images, they can contain many megabytes worth of pixel data.

That lock is kept until you explicitly call its Dispose() method in your code. So be sure that was done before you try to save the image back. In rare cases you may need to create a copy of the image to allow you to dispose the original, use the Bitmap() constructor overload that takes an Image argument.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

What is happening is that when we copy a file from C# code and paste it, even after that program maintains a link/information to original/copied file. This will not happen to files not copied by program or files already present in your folder.

An alternative of this is to fire copy shell/dos command. A way to do that is present in this article

normal copy command in dos is like

copy originalfilenamewithpath destinationfilenamewithpath.

To know more about copy command go to dos and write copy /? and press enter.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
-1

Agree with what Conrad suggested, though there are edge cases where Process Explorer may not be reliable, alternatively you can try Unlocker

Rohit Sharma
  • 6,136
  • 3
  • 28
  • 47