Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<pre>DialogResult result1 = MessageBox.Show("Are you sure to Remove?", "Important Question", MessageBoxButtons.YesNo);
                        if (result1 == DialogResult.Yes)
                        {

                            while (listBox1.SelectedItems.Count > 0)
                            {
                                string FileName = "";
                                FileName = listBox1.SelectedItems[0].ToString();
                                listBox1.Items.Remove(listBox1.SelectedItems[0]);
                                string path = Application.StartupPath + @"\Image\" + FileName;
                                //string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + @"\Image\" + FileName;
                                //string path = Path.GetDirectoryName(Application.ExecutablePath) + @"\Image\" + FileName;
                             
                                //using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
                                //using (StreamWriter str = new StreamWriter(fs))
                                //{

                                if (File.Exists(path))
                                {
                                    File.Delete(path);
                                }
                                //    str.Close();
                                //    fs.Close();
                                //}
                                pictureBox1.Image = null;
                                pictureBox2.Image = null;
                                textBox1.Text = "";
                                MessageBox.Show("File Successfully Removed.");
                            }
                        }
                        else
                        {
                            return;

                        }



What I have tried:

i'm trying to delete a selected image(selected from listbox) from Application folder. so i'm getting error: The process cannot access the file 'filename' because it is being used by another process. please help if anybody knows how do i open and close connection for access I/O operation files.
Posted
Updated 16-Sep-16 5:28am

if (result1 == DialogResult.Yes)
                        {
 
                            if (listBox1.SelectedItems.Count > 0)
                            {
                                string FileName = "";
                                FileName = listBox1.SelectedItems[0].ToString();
                                listBox1.Items.Remove(listBox1.SelectedItems[0]);
                                string path = Application.StartupPath @"\Image\" + FileName;
                                //string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + @"\Image\" + FileName;
                                //string path = Path.GetDirectoryName(Application.ExecutablePath) + @"\Image\" + FileName;
                             
                                //using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
                                //using (StreamWriter str = new StreamWriter(fs))
                                //{

                                if (File.Exists(path))
                                {
                                    File.Delete(path);
                                }
                                //    str.Close();
                                //    fs.Close();
                                //}
                                pictureBox1.Image = null;
                                pictureBox2.Image = null;
                                textBox1.Text = "";
                                MessageBox.Show("File Successfully Removed.");
                            }
                        }


change the while for an if. You are trying to delete it multiple times now.
Also the else is not necesarry you can delete it.
 
Share this answer
 
v2
Comments
Muhammad Rehbar Sheikh 17-Sep-16 3:32am    
yes. but still i got same error.
Wessel Beulink 17-Sep-16 5:11am    
Lol... Maby your listbox is using it? Try to understand te basics. You are using things in the wrong way. Anyhow why are you not deleting after form closing?
Muhammad Rehbar Sheikh 19-Sep-16 4:06am    
will try! thanks.
Quote:
error: The process cannot access the file 'filename' because it is being used by another process.

Pretty explicit! Which word you don't understand in the error message ?

The answer is in the error message: you can't delete a file that is in use by your process or another. And it will not change until the other process stop using that file.
 
Share this answer
 
Comments
Muhammad Rehbar Sheikh 17-Sep-16 3:57am    
i'm unable to find out which process is actually being used this file. my overall way is that >>Browse image file>>Add it to listbox>>Remove selected image from listbox and (from its actual application startuppath bin>debug>image folder)

but when i run my project, & trying to delete this file. the first time was deleted. but second time i'm getting same error. so what should i do know.
The most likely explanation is that you've used Image.FromFile to load the image, or passed the file path to a PictureBox control. This locks the file until your application exits.

Image file is locked when you set the PictureBox Image property to a file[^]

The solution is to use Image.FromStream instead. This only locks the file whilst the stream is open.
C#
using (var stream = File.OpenRead(PathToYourImageFile))
{
    yourPictureBox.Image = Image.FromStream(stream);
}
 
Share this answer
 
Comments
Muhammad Rehbar Sheikh 17-Sep-16 4:04am    
thanks! i've used your code for load image into picturebox. but still got same error while deleting. actually when i run my project, & trying to delete this file. the first time was deleted. but second time i'm getting same error. my overall way is that >>Browse image file>>Add it to listbox>>Remove selected image from listbox and (from its actual application startuppath bin>debug>image folder).

i'm unable to find out which process is actually being used this file. so what should i do know.

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