Strange problem while trying to delete files...

LarryDietz

Member
Joined
Jan 14, 2004
Messages
6
Location
Winter Park, Florida
I am working on a program that loads images into a picturebox, using the following line of code...

Code:
picScreenShot.Image = Image.FromFile(Application.StartupPath + "\screenshots\" + GameList(SelectedGame).InternalName + ".png")

This loads the image just fine. However, when I load a 2nd image (or 3rd, etc...) it continues to hold the previous files open.

If I load 5 images (1 at a time) then go to windows explorer and try to delete one of the first images (which is no longer being displayed) the system will not let me. It give the following error...

An unhandled exception of type System.IO.IOException occurred in microsoft.visualbasic.dll

Additional information: The process cannot access the file "...\screenshots\testfile.png" because it is being used by another process.

It appears to be locking ALL the images it has ever opened, until I shutdown the program.

This is a problem because I need the program to be able to delete some of these files.

Does anyone have any idea why this error is occuring?

Any help would be greatly appreciated.

-Larry
 
You should generally do a cleanup when you work with files, it helps to reduce bugs
so always check if a StreamWriter is still open ( Writer != null ), normally you put that in the "finally" part of your try and catch
 
Well, It was a good idea, but it didnt work :(

I disposed of the image, and loaded the new image. That worked just fine, and the new image displayed. Tehn when I tried to KILL the original image file from the hard drive, it errored, saying the file was in use. :(
 
Back
Top