I googled this error but cannot find anything that relates to my case. I am writing a photo gallery as an exercise, and I am running into this error. I have a Gallery class that includes a List<of Photo objects) collection. My application lets me write
the gallery (just text - photo name, path, etc) to an XML file and read it back. The problem occurs when I read it back twice in succession. Here is the method that does this:
{
string fname = "";
if (lbGallery.SelectedIndex >= 0)
{
flowLayoutPanel1.Controls.Clear();
fname = (string)lbGallery.SelectedItem;
Gallery g = new Gallery(fname, GalleryPath + "\\");
txtGalleryName.Text = fname;
// Creates the ClubPix collection from xml disk file
g.LoadGallery();
// loop through clubpix list and create thumbnails
foreach (var itm in g.PicCollection)
{
if (File.Exists(itm.FsPath + ThumbFolder +itm.PicName))
{
File.Delete(itm.FsPath + ThumbFolder + itm.PicName);
}
CreateThumb(itm.PicName, itm.FsPath, itm.FsPath+ThumbFolder);
}
DisplayThumbNails(g.PicCollection);
}
I added the File.exists/File.Delete code to isolate the error. When I press the load button, the xml file is read and the g.PicCollection is populated. The thumbnails are displayed. If I then just press the Load button again, I get the error below saying
that the file cannot be deleted because it is being used by another process. The thumbnails are loaded into pictureboxes on a flowpanel, I clear those in the code above. So the file is apparantly open, but I don't how? This is on a development machine,
so no other users involved.
ERROR The process cannot access the file 'C:\Users\Public\Pictures\Sample Pictures\thumbs\Chrysanthemum.jpg' because it is being used by another process.
ERROR The process cannot access the file 'C:\Users\Public\Pictures\Sample Pictures\thumbs\Chrysanthemum.jpg' because it is being used by another process.
The simplest way to fix these kind of issues is to see what process is holding this file's handle.
Start by downloading ProcExp from Microsoft. Heres the url
Now, run this tool and click on the Find from the top menu. In the dailog box, enter your file name(complete or partial) and it will list all the processes that are holding a reference to this file
Thank you. That is a useful tool. In this case though, it shows that only my program has the file open. When I reload, even though I have cleared all the controls, apparantly the file remains open. I am not sure how to close it. It is an image in a
picturebox and the picturebox is gone.
glenng910
Member
82 Points
97 Posts
Error Cannot Access File because it is being used by another process
Feb 10, 2010 03:57 PM|LINK
I googled this error but cannot find anything that relates to my case. I am writing a photo gallery as an exercise, and I am running into this error. I have a Gallery class that includes a List<of Photo objects) collection. My application lets me write the gallery (just text - photo name, path, etc) to an XML file and read it back. The problem occurs when I read it back twice in succession. Here is the method that does this:
I added the File.exists/File.Delete code to isolate the error. When I press the load button, the xml file is read and the g.PicCollection is populated. The thumbnails are displayed. If I then just press the Load button again, I get the error below saying that the file cannot be deleted because it is being used by another process. The thumbnails are loaded into pictureboxes on a flowpanel, I clear those in the code above. So the file is apparantly open, but I don't how? This is on a development machine, so no other users involved.
ERROR The process cannot access the file 'C:\Users\Public\Pictures\Sample Pictures\thumbs\Chrysanthemum.jpg' because it is being used by another process.
Kumar Reddi
Star
14340 Points
2619 Posts
Re: Error Cannot Access File because it is being used by another process
Feb 10, 2010 06:39 PM|LINK
The simplest way to fix these kind of issues is to see what process is holding this file's handle.
Start by downloading ProcExp from Microsoft. Heres the url
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx
Now, run this tool and click on the Find from the top menu. In the dailog box, enter your file name(complete or partial) and it will list all the processes that are holding a reference to this file
glenng910
Member
82 Points
97 Posts
Re: Error Cannot Access File because it is being used by another process
Feb 10, 2010 07:38 PM|LINK
Thank you. That is a useful tool. In this case though, it shows that only my program has the file open. When I reload, even though I have cleared all the controls, apparantly the file remains open. I am not sure how to close it. It is an image in a picturebox and the picturebox is gone.
Kumar Reddi
Star
14340 Points
2619 Posts
Re: Error Cannot Access File because it is being used by another process
Feb 10, 2010 07:50 PM|LINK
Show us the CreateThumb method code. It seems like you are not closing the File handle that you open to create the file
glenng910
Member
82 Points
97 Posts
Re: Error Cannot Access File because it is being used by another process
Feb 10, 2010 08:02 PM|LINK
Found this code here in the forums.
private void CreateThumb(string FileName, string path, string thPath) // http://forums.asp.net/p/1206493/2113692.aspx#2113692 { Bitmap bm = (Bitmap)Image.FromFile(path+"//"+FileName); int shtWidth = 0; int shtHeight = 0; shtHeight = bm.Height / ThumbScale; shtWidth = bm.Width / ThumbScale; Bitmap resized = new Bitmap(shtWidth, shtHeight); Graphics g = Graphics.FromImage(resized); g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(bm, new Rectangle(0, 0, resized.Width, resized.Height), 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel); g.Dispose(); resized.Save(thPath + FileName, ImageFormat.Jpeg); txtError.Text = "Thumbs Created"; }Kumar Reddi
Star
14340 Points
2619 Posts
Re: Error Cannot Access File because it is being used by another process
Feb 10, 2010 08:13 PM|LINK
You need to dispose the Bitmap you are creating.
add the following code at the end, before the close of the braces
bm.Dispose();
glenng910
Member
82 Points
97 Posts
Re: Error Cannot Access File because it is being used by another process
Feb 10, 2010 08:30 PM|LINK
I added that and resized.Dispose() also. Didn't help. I am still getting the same error.
private void CreateThumb(string FileName, string path, string thPath) // http://forums.asp.net/p/1206493/2113692.aspx#2113692 { Bitmap bm = (Bitmap)Image.FromFile(path+"//"+FileName); int shtWidth = 0; int shtHeight = 0; shtHeight = bm.Height / ThumbScale; shtWidth = bm.Width / ThumbScale; Bitmap resized = new Bitmap(shtWidth, shtHeight); Graphics g = Graphics.FromImage(resized); g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(bm, new Rectangle(0, 0, resized.Width, resized.Height), 0, 0, bm.Width, bm.Height, GraphicsUnit.Pixel); g.Dispose(); resized.Save(thPath + FileName, ImageFormat.Jpeg); txtError.Text = "Thumbs Created"; resized.Dispose(); bm.Dispose(); }Kumar Reddi
Star
14340 Points
2619 Posts
Re: Error Cannot Access File because it is being used by another process
Feb 10, 2010 09:38 PM|LINK
If this is a web application, first reset your process. You might have references to the file from prior to adding the dispose call.
To reset IIS run iisreset. If its VS web server, kill the Web.WebServer.exe process
glenng910
Member
82 Points
97 Posts
Re: Error Cannot Access File because it is being used by another process
Feb 10, 2010 10:02 PM|LINK
Not a web app. It is a c# Win project
Kumar Reddi
Star
14340 Points
2619 Posts
Re: Error Cannot Access File because it is being used by another process
Feb 10, 2010 10:10 PM|LINK
All you need is Dispose, if you are creating a bitmap file or for that matter any file. Check other parts of your code.
Also, I would recommend going to msdn forums if you are dealing with winforms app.