error in deleting a file with File.Delete() method

前端 未结 3 1286
面向向阳花
面向向阳花 2020-12-12 07:29

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 im

相关标签:
3条回答
  • 2020-12-12 07:47

    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.

    0 讨论(0)
  • 2020-12-12 08:00

    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.

    0 讨论(0)
  • 2020-12-12 08:01

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

    0 讨论(0)
提交回复
热议问题