File lock

J

Jos Roijakkers

At some point in my application I load an image into a picturebox with the
code:

pbGraphic.Image = Image.FromFile("picture.jpg")

Later on in the program I want to rename the file using the code:

pbGraphic.Image = Nothing
My.Computer.FileSystem.RenameFile("picture.jpg","newname.jpg")

but I get an IOException: "The process cannot access the file because it
is being used by another process."
Why is the original file still locked and what can I do to unlock it?

Any help is welcome.
Tnxs
 
A

Andrew Morton

Jos said:
At some point in my application I load an image into a picturebox
with the code:

pbGraphic.Image = Image.FromFile("picture.jpg")

Later on in the program I want to rename the file using the code:

pbGraphic.Image = Nothing
My.Computer.FileSystem.RenameFile("picture.jpg","newname.jpg")

but I get an IOException: "The process cannot access the file because
it is being used by another process."
Why is the original file still locked and what can I do to unlock it?

You have to tell it you've really, really finished with it using
..Dispose():-

pbGraphic.Image = Nothing
pbGraphic.Dispose()
My.Computer.FileSystem.RenameFile("picture.jpg","newname.jpg")

And I'm fairly sure you should be giving the full path for the original and
new names in the RenameFile().

Andrew
 
J

Jos Roijakkers

Unfortunately, adding Dispose() makes no difference.
Re the filenames: the original name should be given with a full path, the
new name should not have the full path.
 
A

Andrew Morton

Jos said:
Unfortunately, adding Dispose() makes no difference.

If you use a program like Unlocker (http://ccollomb.free.fr/unlocker/) you
can see which other process has the file open. I had a problem once with a
source control program obtaining a handle to text files in the same
directory as the app.
Re the filenames: the original name should be given with a full path,
the new name should not have the full path.

I only have .NET 1.1 here, so I don't have the 2.0 docs to hand.
What happens if you use the VB Rename function? Or System.IO.File.Move?
(nothing different, at a guess).

Andrew
 
J

Jos Roijakkers

Thanks Andrew for your help.

The VB Rename function gives an error: "ArgumentException was not handled;
Procedure call or argument is not valid"
System.IO.File.Move gives the same IOException as the RenameFile function.
The Unlocker program shows no other locks on the file than the application
itself.

So I guess I have to unlock the file right after loading it into the picturebox.
But I don't have any clue on how to do that.

Jos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top