Home All Groups Group Topic Archive Search About
Author
15 May 2006 1:43 PM
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

Author
15 May 2006 2:21 PM
Andrew Morton
Jos Roijakkers wrote:
> 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
Author
15 May 2006 2:36 PM
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.

Show quoteHide quote
> Jos Roijakkers wrote:
>
>> 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
>
Author
15 May 2006 3:59 PM
Andrew Morton
Show quote Hide quote
>> Jos Roijakkers wrote:
>>
>>> 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().

Jos Roijakkers wrote:
> 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
Author
15 May 2006 4:31 PM
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

Show quoteHide quote
>>> Jos Roijakkers wrote:
>>>
>>>> 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().
> Jos Roijakkers wrote:
>
>> 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
>
Author
15 May 2006 4:27 PM
Herfried K. Wagner [MVP]
"Jos Roijakkers" <j.roijakk***@qred-it.nl> schrieb:
> 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."

<URL:http://dotnet.mvps.org/dotnet/code/graphics/#ImageNoLock>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
15 May 2006 4:46 PM
Jos Roijakkers
Thanks a lot. I've got it working now.
Vielen Dank!

Jos

Show quoteHide quote
> "Jos Roijakkers" <j.roijakk***@qred-it.nl> schrieb:
>
>> 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."
>>
> <URL:http://dotnet.mvps.org/dotnet/code/graphics/#ImageNoLock>
>