473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

File lock

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","n ewname.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
May 15 '06 #1
6 5476
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","n ewname.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","n ewname.jpg")

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

Andrew
May 15 '06 #2
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.
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","n ewname.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","n ewname.jpg")
And I'm fairly sure you should be giving the full path for the
original and new names in the RenameFile().

Andrew

May 15 '06 #3
>> 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","n ewname.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","n ewname.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
May 15 '06 #4
"Jos Roijakkers" <j.**********@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","n ewname.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/>
May 15 '06 #5
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
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","n ewname.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","n ewname.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

May 15 '06 #6
Thanks a lot. I've got it working now.
Vielen Dank!

Jos
"Jos Roijakkers" <j.**********@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","n ewname.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>

May 15 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Pekka Niiranen | last post by:
Hi, I have used the following example from win32 extensions: -----SCRIPT STARTS---- import win32file import win32con import win32security import pywintypes
4
by: KDB | last post by:
Hi, I am trying the following code to get a write lock on a file. #include <unistd.h> #include <iostream.h> #include <fcntl.h> main() { int fd = open("file",O_RDWR);
8
by: Stephen Corey | last post by:
I'm writing an app that basically just appends text to a text file on a Win2K Server. They fill out a form, click a button, and 1 line is appended to the file. Multiple people will run this app at...
6
by: martin | last post by:
Hi, I have noticed that every aspx page that I created (and ascx file) has an assosiated resource file aspx.resx. However what I would like to do is have a single global resource file for the...
14
by: Gary Nelson | last post by:
Anyone have any idea why this code does not work? FileOpen(1, "c:\JUNK\MYTEST.TXT", OpenMode.Binary, OpenAccess.ReadWrite, OpenShare.Shared) Dim X As Integer For X = 1 To 26 FilePut(1, Chr(X +...
4
by: muttu2244 | last post by:
hi all am trying to write some information into the file, which is located in ftp, and this file can be updated by number of people, but if at all i download a file from the ftp to my local...
1
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access...
13
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it...
5
by: pgdown | last post by:
Hi, I have several processes accessing files from one folder, but only one process should ever access each file. Once one process has the file, no other process should be allowed to access it,...
2
by: WingSiu | last post by:
I am writing a Logging util for my ASP.NET application. I am facing mulit process problem. I developed a class LogFactory, and have a method called Get_Logger to create a FileLogger, which will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.