Any help appreciated.
Image is loaded from a jpeg file (call it "some.jpg") into a
PictureBox control (call it MyPicBox). No problem.
User hits DEL. Event-driven code attempts to delete the file, like so:
My.Computer.FileSystem.DeleteFile(sPath) '(where
sPath is the full path of "some.jpg")
In the old (VB6 for Windows 98) version of the app, I used the
FileSystemObject version of DeleteFile, and this never gave any
trouble.
Now, with VB.NET, in tests under XP and Vista this throws an exception
whose accompanying message reads:
El proceso no puede obtener acceso al archivo 'C:\Users\ThisUser
\Documents\some.jpg' porque está siendo utilizado en otro proceso.
Translated from Spanish: "The process cannot access file <blah> as
it's being used in another process"
The app in question doesn't use multithreading. There is no other
managed process going on when this exception is thrown. This makes me
think that the PictureBox marks the file in some way as "in use" as
long as the picture is being displayed, so that the operating system
can't move or delete or rename or do anything with it until it's freed
up. Tests show that it doesn't matter which file access system I use,
the result is the same. Tests also show that trying to change the
file's ReadOnly or other properties from code doesn't work either.
Now comes the surprising part. As a workaround, I tried this:
MyPicBox.Image.Dispose() 'get rid of image from PictureBox
control to free up file
My.Computer.FileSystem.DeleteFile(sPath)
'plus some code to load a plain grey bitmap into MyPicBox,
replacing what was there before
It still throws an exception - sometimes. When it does, putting it in
a loop and trying a few hundred times (to give the OS time to catch
up) doesn't make any difference - usually. Further experimentation
suggests that loading a new jpg from another file in the PictureBox
doesn't (usually) solve the problem, and that the time lapse before
the system decides that "some.jpg" is free to move or delete varies
wildly between a few seconds and five to ten minutes.
What's going on, and how to solve it?
expvb - 21 Apr 2008 15:51 GMT
This newsgroup is for VB6 and lower. For VB.Net, please look in newsgroups
with "dotnet" or "vsnet" in the name.
gerardianlewis@yahoo.com - 22 Apr 2008 02:44 GMT
> This newsgroup is for VB6 and lower. For VB.Net, please look in newsgroups
> with "dotnet" or "vsnet" in the name.
Thanks, and apologies.