Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my program I have to delete an image file before I exit the program
but I could not
C#
string str ="MyPicture.gpj";//<-- str is global string

   pictureBox1.Image = Image.FromFile(str);

   //pressing the button to call the next event

   private void ExitClick(object sender, EventArgs e)
   {
    //pictureBox1.Image = null;
    if(File.Exists(str)) File.Delete(str);// <-------- I have an error here
    Application.Exit();
   }
Posted
Updated 24-Jan-11 4:08am
v3
Comments
Gregory Gadow 24-Jan-11 10:12am    
It would be helpful to see what the actual error message is. My first thought is that the image file is in use from something else in the app.

Your problem is probably because you are using the Image.FromFile() method. There is a known problem with this method, in that it locks the file.

Use the Image.FromStream()[^] method instead and all should be well.
 
Share this answer
 
Comments
Nish Nishant 24-Jan-11 10:13am    
Voted 5, proposed as answer.
Pravin Patil, Mumbai 24-Jan-11 10:33am    
Take my 5
fjdiewornncalwe 24-Jan-11 14:24pm    
+5. Agreed
Problem is that the image in picture still holds a handle to the file. Try to dispose the the image first:

C#
Image img = pictureBox1.Image;
pictureBox1.Image = null;
at this point there should be no other references to the image
img.Dispose();
img = null;
// Try to do file deletion now
File.Delete(str);


Hope that helps!

Best Regards,
Manfred
 
Share this answer
 
Comments
Nish Nishant 24-Jan-11 10:14am    
Not if you use Image.FromFile. The file remains locked until the app exits (which is quite bizarre really I guess).
This is by design.

See http://support.microsoft.com/kb/309482[^]
 
Share this answer
 
I have to comment on my previous question and the answered that I have chosen
The answer were good if the pictureBox1 is attached to only one image file
But
If the the pictureBox1 is attached to more than one file , I still can not delete those image files
Therefore I preferred to use the filestream method, I used it and it were much better than the
following
C#
pictureBox1.Image = Image.FromFile(FruitName[0]+".jpg");
img = pictureBox1.Image;
pictureBox1.Image = null;
img.Dispose();
img = null;
if(File.Exists(FruitName[0] + ".jpg")) File.Delete(FruitName[0]+".jpg");
//------End------------------------------------------------------------------------



The following is much better

C#
Private void UpButtonClick((object sender, EventArgs e)
{
j=…..
fs = new System.IO.FileStream(FruitName[j]+ ".jpg", FileMode.Open, FileAccess.Read);
pictureBox1.Image = System.Drawing.Image.FromStream(fs);
fs.Close();
}
Private void DownButtonClick((object sender, EventArgs e)
{
   j=…..
   fs = new System.IO.FileStream(FruitName[j] + ".jpg", FileMode.Open, FileAccess.Read);
   pictureBox1.Image = System.Drawing.Image.FromStream(fs);
   fs.Close();
}

private void ExitClick(object sender, EventArgs e)
{
   for (i = 0; i < TotalFruit; i++)
   {
      if(File.Exists(FruitName[i] + ".jpg"))
      File.Delete(FruitName[i] + ".jpg");
   }
      
   Application.Exit();
}
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900