954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Deleting File Through C# Program.

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

VidyaYadav
Newbie Poster
10 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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;
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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

VidyaYadav
Newbie Poster
10 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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 :)

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

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

VidyaYadav
Newbie Poster
10 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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();
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

posted by mistake

VidyaYadav
Newbie Poster
10 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

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);
Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

Aah, no problem.

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

Ryshad
Nearly a Posting Virtuoso
1,307 posts since Aug 2009
Reputation Points: 512
Solved Threads: 246
 

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

VidyaYadav
Newbie Poster
10 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Yoy can just try this to delete files.

http://longpathtool.com/

technext
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You