Sanjeewa
Sanjeewa

Reputation: 83

"the process cannot access the file because it is being used by another process" when trying to delete file

when deleting files one by one error is generated as "the process cannot access the file ' because it is being used by another process when trying to delete file"

code : any suggestions for delete files like this ?

      private void DeleteFilesFromDestination()
      {
           string consolidatedFolder = System.Configuration.ConfigurationManager.AppSettings["path"].ToString();

           foreach (String file in ListBoxDeleteFiles.Items)
           {
                try
                {
                     // delete each selected files from the specified TargetFolder 
                     if (System.IO.File.Exists(consolidatedFolder + @"\" + System.IO.Path.GetFileName(file)))
                     {
                         proc.WaitForExit(); 
                         System.IO.File.Delete(consolidatedFolder + @"\" + System.IO.Path.GetFileName(file));
                     }
                }
                catch (Exception ex)
                {
                     MessageBox.Show("Error Could not Delete file from disk " + ex.Message, "Shipment Instruction",
                         MessageBoxButtons.OK, MessageBoxIcon.Error);
                     return;
                }

           }
      }

NB : the image will be loaded to a flowlayout pannel like this

 //Open the files to see
          private void ListBoxSourceFiles_Click(object sender, EventArgs e)
          {
               try
               {
                    if (ListBoxSourceFiles.SelectedItem != null || !ListBoxSourceFiles.SelectedItem.Equals(string.Empty))
                    {
                         //MessageBox.Show("Selected " + ListBoxSourceFiles.SelectedItem);
                         PictureBox pb = new PictureBox();
                         Image loadedImage = null;
                         loadedImage = Image.FromFile(ListBoxSourceFiles.SelectedItem.ToString());
                         pb.Height = loadedImage.Height;
                         pb.Width = loadedImage.Width;
                         pb.Image = loadedImage;
                         flowLayoutPanel1.Controls.Clear();
                         flowLayoutPanel1.Controls.Add(pb);
                    }
               }
               catch (Exception ex)
               {
                    MessageBox.Show(ex.Message, "Ship Instruction",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
               }
          }

Upvotes: 2

Views: 8111

Answers (4)

joe
joe

Reputation: 8654

You will not be able to delete any file when is is locked by another process.

You first have to find out which process locks the file.
This is possible with SysInternals ProcessExplorer. Use the "Find handle or DLL" function.

Upvotes: 2

Morteza Allahyari
Morteza Allahyari

Reputation: 1120

pb.Image.Dispose();
pb.Dispose();

After steps above, tou can use picture again

Upvotes: 0

Jim Mischel
Jim Mischel

Reputation: 134105

You don't say specifically what file you're trying to delete, but from your question it sounds like you're trying to delete the image file that you loaded. If that's the case, then you have a problem. The documentation for Image.FromFile says:

The file remains locked until the Image is disposed.

If you need the ability to delete the file, you'll want to copy the image after you've loaded it, and use that image in your PictureBox. Then you can dispose the loaded image, thereby unlocking the file.

Upvotes: 6

Martin Liversage
Martin Liversage

Reputation: 106926

If the file is in use you cannot delete it. However, if you for some reason really want to delete it and you are unable to stop the process that is locking the file (like when uninstalling an application) you can schedule the file for deletion the next time the operating system is restarted. These scheduled deletions are performed before any process is able to lock the file.

You have to use the MoveFileEx Windows API using a null new file name and the flag MOVEFILE_DELAY_UNTIL_REBOOT. How to do that from C# is explained in an answer to the Stack Overflow question “MoveFile” function in C# (Delete file after reboot) C#.

Upvotes: 0

Related Questions