Deleting File Through C# Program.

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 10
Reputation: VidyaYadav is an unknown quantity at this point 
Solved Threads: 0
VidyaYadav's Avatar
VidyaYadav VidyaYadav is offline Offline
Newbie Poster

Deleting File Through C# Program.

 
0
  #1
Oct 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven
 
0
  #2
Oct 7th, 2009
Your process is the one that uses file, you need to set image to null use something like this,

  1. using(var img = Image.FromFile(filename).GetThumbnailImage(GetWitdth(filename, GetHeight(filename, 200)), GetfHeight(filename, 200), null, new IntPtr()))
  2. pictureBox1.Image = img;
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: VidyaYadav is an unknown quantity at this point 
Solved Threads: 0
VidyaYadav's Avatar
VidyaYadav VidyaYadav is offline Offline
Newbie Poster
 
0
  #3
Oct 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 465
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 92
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Pro in Training
 
0
  #4
Oct 7th, 2009
If you want to repalce the file if it exists, rather than deleting it then copying it, try changing it to this:

  1. 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
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: VidyaYadav is an unknown quantity at this point 
Solved Threads: 0
VidyaYadav's Avatar
VidyaYadav VidyaYadav is offline Offline
Newbie Poster

Error Due To Image Loaded In Flowlayoutpanel

 
0
  #5
Oct 7th, 2009
Originally Posted by Ryshad View Post
If you want to repalce the file if it exists, rather than deleting it then copying it, try changing it to this:

  1. 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
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 465
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 92
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Pro in Training
 
1
  #6
Oct 7th, 2009
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:

  1. System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  2. PictureBox1.Image = System.Drawing.Image.FromStream(fs);
  3. fs.Close();
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: VidyaYadav is an unknown quantity at this point 
Solved Threads: 0
VidyaYadav's Avatar
VidyaYadav VidyaYadav is offline Offline
Newbie Poster

Error Due To Image Loaded In Flowlayoutpanel

 
0
  #7
Oct 7th, 2009
posted by mistake
Last edited by VidyaYadav; Oct 7th, 2009 at 7:43 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 465
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 92
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Pro in Training
 
0
  #8
Oct 7th, 2009
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:

  1. PictureBox pb = new PictureBox();
  2. System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read);
  3. Image loadedImage = Image.FromStream(fs);
  4. fs.Close();
  5. pb.Height = 245;
  6. pb.Width = 245;
  7. pb.BorderStyle = BorderStyle.None;
  8. pb.Image = loadedImage;
  9. pb.Name = "pic" + j.ToString();
  10. pb.Click += new EventHandler(picture_click);
  11. pb.SizeMode = PictureBoxSizeMode.StretchImage;
  12. flowLayoutPanel1.Controls.Add(pb);
Last edited by Ryshad; Oct 7th, 2009 at 7:57 am.
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 465
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 92
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Pro in Training
 
0
  #9
Oct 7th, 2009
Aah, no problem.

I take it the code fixed the issue then? If so, remember to mark the thread as solved.
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: VidyaYadav is an unknown quantity at this point 
Solved Threads: 0
VidyaYadav's Avatar
VidyaYadav VidyaYadav is offline Offline
Newbie Poster
 
0
  #10
Oct 7th, 2009
Originally Posted by Ryshad View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 338 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC