# VB.Net file is use error

**URL:** <https://boards.straightdope.com/t/vb-net-file-is-use-error/398725>\
**Category:** Factual Questions\
**Created:** [April 4, 2007, 4:30pm UTC](https://boards.straightdope.com/t/vb-net-file-is-use-error/398725 "2007-04-04T16:30:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![LateComer](https://avatars.discourse-cdn.com/v4/letter/l/b5ac83/32.png) [@LateComer](https://boards.straightdope.com/u/LateComer)\
**Post date:** [April 4, 2007, 4:30pm UTC](https://boards.straightdope.com/t/vb-net-file-is-use-error/398725/1 "2007-04-04T16:30:54Z")

</div>

I’m working on a [vb.net](http://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:

```auto

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?

---

<div class="post-metadata">

**Author:** ![LilShieste](https://avatars.discourse-cdn.com/v4/letter/l/9f8e36/32.png) [@LilShieste](https://boards.straightdope.com/u/LilShieste)\
**Post date:** [April 4, 2007, 7:31pm UTC](https://boards.straightdope.com/t/vb-net-file-is-use-error/398725/2 "2007-04-04T19:31:22Z")

</div>

[QUOTE=LateComer]  
I’m working on a [vb.net](http://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:

```auto

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:

> [@](#):
>
> The file remains locked until the Image is disposed.

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

```auto

Me.picThumb.Image.Dispose()

```

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

---

<div class="post-metadata">

**Author:** ![LateComer](https://avatars.discourse-cdn.com/v4/letter/l/b5ac83/32.png) [@LateComer](https://boards.straightdope.com/u/LateComer)\
**Post date:** [April 5, 2007, 1:35am UTC](https://boards.straightdope.com/t/vb-net-file-is-use-error/398725/3 "2007-04-05T01:35:12Z")

</div>

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

```auto

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.

```auto

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:

```auto

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

```

thanks for your help.

---

<div class="post-metadata">

**Author:** ![LilShieste](https://avatars.discourse-cdn.com/v4/letter/l/9f8e36/32.png) [@LilShieste](https://boards.straightdope.com/u/LilShieste)\
**Post date:** [April 5, 2007, 7:04am UTC](https://boards.straightdope.com/t/vb-net-file-is-use-error/398725/4 "2007-04-05T07:04:38Z")

</div>

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:

```auto

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

---

<div class="post-metadata">

**Author:** ![LateComer](https://avatars.discourse-cdn.com/v4/letter/l/b5ac83/32.png) [@LateComer](https://boards.straightdope.com/u/LateComer)\
**Post date:** [April 5, 2007, 1:05pm UTC](https://boards.straightdope.com/t/vb-net-file-is-use-error/398725/5 "2007-04-05T13:05:06Z")

</div>

[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:

```auto

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.

---

<div class="post-metadata">

**Author:** ![LilShieste](https://avatars.discourse-cdn.com/v4/letter/l/9f8e36/32.png) [@LilShieste](https://boards.straightdope.com/u/LilShieste)\
**Post date:** [April 5, 2007, 3:12pm UTC](https://boards.straightdope.com/t/vb-net-file-is-use-error/398725/6 "2007-04-05T15:12:55Z")

</div>

[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. 😉  
LilShieste
