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\\us
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
}
Try this...
http://www.lockergnome.com/blade/2006/11/28/windows-error-message-error-deleting-file-or-folder/
It will allow you to delete files or folder.
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.