user980956
user980956

Reputation: 23

Cannot Delete File using C# Windows Forms Application

I am opening a file from a location (say, X) in a PictureBox.

After that, if the user clicks on a button say "btn1" then I copy the file from Location X to Location Y using File.Copy().

After that I set the Picturebox1.Image=null; ...and use File.Delete(X);

At this point Visual studio shows me the exception that The file cannot be deleted as it is being used by another process.

Any ideas as to why would that be?

Upvotes: 1

Views: 1677

Answers (3)

SLaks
SLaks

Reputation: 888223

Call Picturebox1.Image.Dispose().

Upvotes: 2

kleinohad
kleinohad

Reputation: 5912

You can use File.Move() as workaround http://msdn.microsoft.com/en-us/library/system.io.file.move.aspx

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 191058

Make sure you are disposing the original Image.

Otherwise, another program has a handle on it.

Upvotes: 0

Related Questions