2

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;
               }
          }
sloth
  • 99,095
  • 21
  • 171
  • 219
Sanjeewa
  • 83
  • 2
  • 10
  • You can use a tool like "Unlocker" to find out, which process is locking the file. If you're opening the file yourself if they're images from the combobox, you could try to copy the file into a (memory) bitmap first, and not directly load it from the image. Just an idea, thus just a comment. – Akku Jul 29 '13 at 11:08
  • Checked if the file has remained open in some other program. you can trying closing all the instances, if on a multiple user machine, check if any other user is accessing it. then try again. – Vinay Pratap Singh Bhadauria Jul 29 '13 at 11:29
  • possible duplicate of [How to do one image file, save and delete functions continously in C#](http://stackoverflow.com/questions/17485453/how-to-do-one-image-file-save-and-delete-functions-continously-in-c-sharp) – Cody Gray - on strike May 04 '14 at 06:06

4 Answers4

6

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.

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
2

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
joe
  • 8,344
  • 9
  • 54
  • 80
0

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#.

Community
  • 1
  • 1
Martin Liversage
  • 104,481
  • 22
  • 209
  • 256
0
pb.Image.Dispose();
pb.Dispose();

After steps above, tou can use picture again