Problem with deleting files

rahavtom

Well-known member
Joined
Apr 2, 2004
Messages
63
Hello!

I use VB.NET and I have a form which contain a picturebox control that I fill with a choosen image by:

Me.picImage1.Image = FromFile("1.jpg")

Then I have a different button which suppose to clear the picturebox control, and delete the file itself from the original windows directory.

The problem is that the picturebox is being cleared, but the file cant be deleted. It seems that its locked or something, and Im able to delete it only after I close the running program.

Does anyone know what can be done in order to delete the file during runtime?

Thanks!
Tom.
 
rahavtom said:
This is the code I use to delete the file:

system.IO.File.Delete("1.jpg")

You need to put the path inside of the quotes.

Example -
System.IO.File.Delete("C:\Program Files\1.jpg")
 
Simcoder said:
You need to put the path inside of the quotes.

Example -
System.IO.File.Delete("C:\Program Files\1.jpg")

Sure, in my source I put the full path as needed. I have a long path that I create in runtime, such "c:\" & ApplicationPath & PictureNumber & ".jpg"
I didnt write it here because it was too complicated, and I prefered writing more simple picture name as example.

Is there any other reason for that?
 
rahavtom said:
Sure, in my source I put the full path as needed. I have a long path that I create in runtime, such "c:\" & ApplicationPath & PictureNumber & ".jpg"
I didnt write it here because it was too complicated, and I prefered writing more simple picture name as example.

Is there any other reason for that?


Can you give me the error message that is being displayed when you try to delete the file?
 
One More Thing, Assuming Your Paths Are Correct, It Seems To Be That You Cant Delete The File Because It Is Still In Use By Another Process. You Should Release The File From Memory, One Simple Way To Achieve This Would
Be To Type "GC.Collect" Right Before You Delete The Image. That Should Work.
 
PlausiblyDamp said:
http://www.computerhelp.forum/showthread.php?t=83882&highlight=delete+picturebox may be worth a look, also try to avoid calling GC.Collect() if at all possible - it can adversely affect performance.

Well, the original error message I got was:
The file cant be deleted because it is still in use by another process...

I added the line: Me.picImage1.Image.Diapose before I delete the file, so my code looks like this:

Me. picImage1.Image.Dispose
Me. picImage1.Image = Nothing
File.Delete("...")

The Dispose solved part of the problem, because Im able to delete the file whenever I want, but only when I still at that form.
When I cloe the form and open it again, and then try to delete the file, I get the same error message (still in use...).
I tried the GC.Collect as well but it didnt work...

Thanks,
Tom.
 
rahavtom said:
Hello!

Me.picImage1.Image = FromFile("1.jpg")

Tom.

Im not sure, but go ahead and put this code in right before you put a picture into the image. That should always dispose of the last used image. Then try
deleting the file again and see what happens.

If Not picImage1.Image Is Nothing Then
picImage1.Image.Dispose()
End If
 
Back
Top