Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 can't be deleted. It seems that it's locked or something, and I'm 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.

Posted
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")

Whatever thy hand findest to do, do it with all thy heart - Jesus
Posted
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 didn't 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?

Posted
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 didn't 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?

Whatever thy hand findest to do, do it with all thy heart - Jesus
Posted

One More Thing, Assuming Your Paths Are Correct, It Seems To Be That You Can't 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.

Whatever thy hand findest to do, do it with all thy heart - Jesus
Posted
http://www.xtremedotnettalk.com/showthread.php?t=83882&highlight=delete+picturebox may be worth a look' date=' also try to avoid calling GC.Collect() if at all possible - it can adversely affect performance.[/quote']

 

Well, the original error message I got was:

The file can't 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 I'm 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 didn't work...

 

Thanks,

Tom.

Posted
Hello!

 

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

 

Tom.

 

I'm 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

Whatever thy hand findest to do, do it with all thy heart - Jesus

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...