locked
One problem When Delete One Picture RRS feed

  • Question

  • Hi
    My Program Show all picture in a Directore , But i Can t Delete One picture
    Error For delete Picture Is 

    The process cannot access the file 'D:\Documents and Settings\Morteza Fatehi\My Documents\My Pictures\.NET Data Demo.1.jpg' because it is being used by another process.

    please download  program For Solved this Problem.

    Link Of Program

    Tanx
    Tuesday, February 10, 2009 2:48 PM

Answers

  • The problem is not necessarily with your code but the fact that some process has the file locked.  It is a condition you'll have to deal with so you need to decide how your app will handle it.

    Looking at your code I do see one place where your process will lock the file though.  You have code to load the picturebox with the image from the file.  This puts a lock on the file.  You need to (just giving it a cursory look) instead (using a using statement) get the image from the file, create a copy of the image using Clone that you then assign to the picturebox.  You can then exit the using block and allow the image (and file) to be cleaned up.

    Michael Taylor - 2/10/09
    http://p3net.mvps.org
    • Proposed as answer by Guo Surfer Thursday, February 12, 2009 7:30 AM
    • Marked as answer by Guo Surfer Tuesday, February 17, 2009 2:16 AM
    Tuesday, February 10, 2009 3:08 PM
  • Hi Morteza,

    Michael has provided a very helpful answer.

    I'm sorry that there is an error and I can not download your program.

    If you want to delete image had been used in picturebox, then you can try the following code snippet.

            string picPath = @"c:\1.jpg";  
     
            //show image in picturebox  
            private void button1_Click(object sender, EventArgs e)  
            {  
                Stream stream = File.Open(picPath, FileMode.Open);  
                Image image = Image.FromStream(stream);  
                stream.Close();  
                pictureBox1.Image = image;   
            }  
     
            //delete the image  
            private void button2_Click(object sender, EventArgs e)  
            {  
                pictureBox1.Image = null;  
                File.Delete(picPath);  
            } 

    Also, you can use Bitmap to do so.
    Code Snippet:
            string picPath = @"c:\1.jpg";  
            Bitmap btm;  
     
            //show image in picturebox  
            private void button1_Click(object sender, EventArgs e)  
            {  
                btm = new Bitmap(picPath);  
                this.pictureBox1.Image = btm;  
            }  
     
            //delete the image   
            private void button2_Click(object sender, EventArgs e)  
            {  
                btm.Dispose();  
                pictureBox1.Image = null;  
                File.Delete(picPath);  
            } 

    Have a try.
    If you still have problem, please provide your sample code.
    Thank you!

    Best regards,
    Guo
    • Marked as answer by Guo Surfer Tuesday, February 17, 2009 2:16 AM
    Thursday, February 12, 2009 7:29 AM

All replies

  • Isn't the error self explanatory? You'd get the same error if you tried to delete it via Windows Explorer.

    http://blog.voidnish.com
    Tuesday, February 10, 2009 3:06 PM
  • The problem is not necessarily with your code but the fact that some process has the file locked.  It is a condition you'll have to deal with so you need to decide how your app will handle it.

    Looking at your code I do see one place where your process will lock the file though.  You have code to load the picturebox with the image from the file.  This puts a lock on the file.  You need to (just giving it a cursory look) instead (using a using statement) get the image from the file, create a copy of the image using Clone that you then assign to the picturebox.  You can then exit the using block and allow the image (and file) to be cleaned up.

    Michael Taylor - 2/10/09
    http://p3net.mvps.org
    • Proposed as answer by Guo Surfer Thursday, February 12, 2009 7:30 AM
    • Marked as answer by Guo Surfer Tuesday, February 17, 2009 2:16 AM
    Tuesday, February 10, 2009 3:08 PM
  • very tanx
    But I am new programer in dot net, please edit my code , tanx for your help

    Tuesday, February 10, 2009 3:40 PM
  • please show your Idea with small sample , very tanx

    Tuesday, February 10, 2009 4:00 PM
  • Hi Morteza,

    Michael has provided a very helpful answer.

    I'm sorry that there is an error and I can not download your program.

    If you want to delete image had been used in picturebox, then you can try the following code snippet.

            string picPath = @"c:\1.jpg";  
     
            //show image in picturebox  
            private void button1_Click(object sender, EventArgs e)  
            {  
                Stream stream = File.Open(picPath, FileMode.Open);  
                Image image = Image.FromStream(stream);  
                stream.Close();  
                pictureBox1.Image = image;   
            }  
     
            //delete the image  
            private void button2_Click(object sender, EventArgs e)  
            {  
                pictureBox1.Image = null;  
                File.Delete(picPath);  
            } 

    Also, you can use Bitmap to do so.
    Code Snippet:
            string picPath = @"c:\1.jpg";  
            Bitmap btm;  
     
            //show image in picturebox  
            private void button1_Click(object sender, EventArgs e)  
            {  
                btm = new Bitmap(picPath);  
                this.pictureBox1.Image = btm;  
            }  
     
            //delete the image   
            private void button2_Click(object sender, EventArgs e)  
            {  
                btm.Dispose();  
                pictureBox1.Image = null;  
                File.Delete(picPath);  
            } 

    Have a try.
    If you still have problem, please provide your sample code.
    Thank you!

    Best regards,
    Guo
    • Marked as answer by Guo Surfer Tuesday, February 17, 2009 2:16 AM
    Thursday, February 12, 2009 7:29 AM