I have a program that displays an image from an image file – in this case a bitmap file – using Image.FromFile(Filename). I want to delete the file before closing the program. But it was failing when it tried to use My.Computer.FileSystem.DeleteFile(FullName) with the error “The process cannot access the file ‘<filename>’ because it is being used by another process.”
The solution is to dispose of the image.
The reason is that Image.FromFile places a lock on the file and holds it open. Disposing of the image releases the lock and closes the file.