Hi,
In my application i am taking multiple image files from user using openfiledialog and display those image files in flowlayoutpanel. After pressing on Insert button i used to copy all those images in a folder (C:\Images). When there is no file exists the file get copied easily, but if it exists i delete the file, but while deleting file it shows error
"The process cannot access the file"C:\Images\xyz.jpg" because it is being used by another process"

The code i used is
if (File.Exists(destFileName))
{
File.Delete(destFileName);
File.Copy(sourceFileName, destFileName);
}
else
File.Copy(sourceFileName, destFileName);


Is it because i display the images in flowlayoutpanel while deleting. But before execting above code i clears all controls added in the flowlayoutpanel. Still the error occurs.. What should i do?
Regards,
Vidya

Your process is the one that uses file, you need to set image to null use something like this,

using(var img = Image.FromFile(filename).GetThumbnailImage(GetWitdth(filename, GetHeight(filename, 200)), GetfHeight(filename, 200), null, new IntPtr()))
pictureBox1.Image = img;

I use the following code to add pictureboxes,

PictureBox pb = new PictureBox();
Image loadedImage = Image.FromFile(file);
pb.Height = 245;
pb.Width = 245;
pb.BorderStyle = BorderStyle.None;
pb.Image = loadedImage;
pb.Name = "pic" + j.ToString();
pb.Click += new EventHandler(picture_click);
pb.SizeMode = PictureBoxSizeMode.StretchImage;
flowLayoutPanel1.Controls.Add(pb);

Should i do something like this,
loadedImage=null;

Regards,
Vidya

If you want to repalce the file if it exists, rather than deleting it then copying it, try changing it to this:

File.Copy(sourceFileName, destFileName, true);

The boolean at the end sets whether files can be replaced. By setting it to true it should overwrite the file if it already exists :)

If you want to repalce the file if it exists, rather than deleting it then copying it, try changing it to this:

File.Copy(sourceFileName, destFileName, true);

The boolean at the end sets whether files can be replaced. By setting it to true it should overwrite the file if it already exists :)

Sorry, but still error occurs,
But the error occurs only if the image(the path which i want to delete) is loaded in the flowlayoutpanel.

Means, if the image is loaded in the flowlayoutpanel it is not deleted.:(

Vidya

had a quick search around the Micrsoft Support site, if you use FromFile in code to set an image file to a picturebox it locks the file. You can avoid this by using a filestream to retrieve the image:

System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
            PictureBox1.Image = System.Drawing.Image.FromStream(fs);
            fs.Close();
commented: Thank U So Much +1

posted by mistake

Have you implemented the code i gave you? This solves the problem as it reads the file without locking it.
Your code should look like this:

PictureBox pb = new PictureBox();
System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read);
Image loadedImage = Image.FromStream(fs);                       
fs.Close();
pb.Height = 245;
pb.Width = 245;
pb.BorderStyle = BorderStyle.None;
pb.Image = loadedImage;
pb.Name = "pic" + j.ToString();
pb.Click += new EventHandler(picture_click);
pb.SizeMode = PictureBoxSizeMode.StretchImage;
flowLayoutPanel1.Controls.Add(pb);

Aah, no problem.

I take it the code fixed the issue then? If so, remember to mark the thread as solved.

Aah, no problem.

I take it the code fixed the issue then? If so, remember to mark the thread as solved.

Thank U So Much.....
Finally everything runs fine....

Vidya

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.