VB.Net file is use error

I’m working on a vb.net project and have a slight problem. I have a datagrid with a directory listing of bitmap files. When a row is selected, the file is shown in a picture box as a preview.

The picture box image is set:


Me.picThumb.Image = System.Drawing.Image.FromFile(fileString)

This works great. I want to be able to delete the bitmap file selected in the datagrid. I get a file in use error. I figured that this was because the picturebox was using the file, so I tried changing the picturebox image before the delete but this didn’t fix the problem. Through troubleshooting I found that any picture that was previewed at any time while the program is running is locked and can’t be deleted until I exit the program, whether I try to delete by hand from a command prompt or through Explorer.

Any thoughts?

[QUOTE=LateComer]
I’m working on a vb.net project and have a slight problem. I have a datagrid with a directory listing of bitmap files. When a row is selected, the file is shown in a picture box as a preview.

The picture box image is set:


Me.picThumb.Image = System.Drawing.Image.FromFile(fileString)

This works great. I want to be able to delete the bitmap file selected in the datagrid. I get a file in use error. I figured that this was because the picturebox was using the file, so I tried changing the picturebox image before the delete but this didn’t fix the problem. Through troubleshooting I found that any picture that was previewed at any time while the program is running is locked and can’t be deleted until I exit the program, whether I try to delete by hand from a command prompt or through Explorer.

Any thoughts?
[/QUOTE]

According to the MSDN documentation:

So, just add the following line of code, sometime before you try deleting the file.:



Me.picThumb.Image.Dispose()


I hope that helps. If not, just post back. :slight_smile:
LilShieste

[QUOTE=LilShieste]
So, just add the following line of code, sometime before you try deleting the file.:



Me.picThumb.Image.Dispose()


[/QUOTE]
Tried and failed. That’s what’s so frustrating. I am convinced that it’s the preview since I also have text files in the directory without preview and they delete without problem.

Here’s what I do before trying the delete command. This is basically the culmination of all of the things I tried to free up that file, none of which worked. I even put the sleeps in to give the OS time to do its business.


Me.picThumb.Image.Dispose()
System.Threading.Thread.Sleep(2000)
Me.picThumb.Image = Nothing
Me.picThumb.Image = System.Drawing.Bitmap.FromFile(filePath)
Me.picThumb.Image.Dispose()
System.Threading.Thread.Sleep(2000)

Here is the error I get on the command line with the delete command:


The process cannot access the file because it is being used by another process.

thanks for your help.

Hmm, that is pretty bizarre. :dubious: I can see why this is so frustrating.

Are you using .Net 2.0? If so, then try the following approach:



Me.picThumb.Load(filePath)


As a side question: is it possible that you have any other controls accessing that same image, during the preview? Or is the picture box the only object referencing it?
LilShieste

[QUOTE=LilShieste]
Hmm, that is pretty bizarre. :dubious: I can see why this is so frustrating.

Are you using .Net 2.0? If so, then try the following approach:



Me.picThumb.Load(filePath)


[/quote]
This doesn’t work. I’m using 1.1

[QUOTE=LilShieste]
As a side question: is it possible that you have any other controls accessing that same image, during the preview? Or is the picture box the only object referencing it?
LilShieste
[/QUOTE]
No, only the picture box. The only other reference to the bmp files is the datagrid which is filled with just a directory listing. I’m going to try it on a machine with a newer version of the framework if I can find such a machine. If it still doesn’t work, I’m abandoning that function and moving on with my life.

Thanks for your help.

[QUOTE=LateComer]
This doesn’t work. I’m using 1.1
No, only the picture box. The only other reference to the bmp files is the datagrid which is filled with just a directory listing. I’m going to try it on a machine with a newer version of the framework if I can find such a machine. If it still doesn’t work, I’m abandoning that function and moving on with my life.

Thanks for your help.
[/QUOTE]

Sorry I couldn’t help much. If you do figure out a solution at some point, then I’d be curious to hear about it.

Otherwise, moving on with your life is probably a good idea, if it’s a valid option. :wink:
LilShieste