Greetings. The situation is this: there are several forms; on one of the forms, pictures with fabrics are dynamically loaded. in the process, you can remove one of the pictures. implemented this way - if you click on the picture, a new form will fly out with the same larger picture and the delete button. when you click on it, the name of the picture is entered into a separate class and when the form is closed it is loaded into Main_FormClosed . Code :

 try { string CompletePatchToEXE = Directory.GetCurrentDirectory(); DirectoryInfo dir = new DirectoryInfo(CompletePatchToEXE + @"\\materials\\"); string delNAME = OrderData.deletFiles[0].ToString() + ".jpg"; foreach (FileInfo file in dir.GetFiles()) { if (file.Name == delNAME) { file.Delete(); } } } catch (Exception ex) { MessageBox.Show(ex.Message, ex.Source); } 

And the problem is that the specified file is still used by the application and cannot delete it. How to be, tell me? Thank.

Image files are dynamically loaded from the @ "materials /" directory and assigned as background images for picchers like this:

 PB.BackgroundImage = Image.FromFile("materials/" + ListName[i] + ".jpg"); 
  • It is necessary to use using when working with a file. Do not block the file for a long time. You can write two or three attempts to delete a file using ILDE and waiting in 1 second. You can read procedures in static methods and use lock to avoid "multitasking" conflicts. - nick_n_a
  • it is possible in more detail about using? what line to wrap? - Sergey
  • The line where you read the file. For example using ( File f = File.Open()) {}; - nick_n_a
  • You do not quite understand. image files are dynamically loaded from the @ "materials /" directory and are assigned as background images for pickers like this: PB.BackgroundImage = Image.FromFile ("materials /" + ListName [i] + ".jpg"); - Sergey
  • one
    like this static void DoDeleteFiles() { try { .... file.Delete } } and so static Image DoLoadImage(string filename) { return Image.FromFile(filename); } static Image DoLoadImage(string filename) { return Image.FromFile(filename); } - nick_n_a

1 answer 1

In the comments you have written:

 PB.BackgroundImage = Image.FromFile("materials/" + ListName[i] + ".jpg"); 

The image is loaded and assigned to the BackgroundImage property. This is where its handle is held. To remove an image, you need to release its handle.

For example, it can be done like this:

 Image imageCopy; using (var imageSource = Image.FromFile("materials/" + ListName[i] + ".jpg") imageCopy = new Bitmap(imageSource); PB.BackgroundImage = imageCopy; 

In this code, a copy of the image is created - its handle will not be bound to the file. And when you exit the using block, the Dispose method will be called, which will release the resource — the file of the original image. After which it can be removed.

PS The deletion itself is performed in one line:

 new FileInfo(delNAME).Delete(); 

The cycle is not needed.

  • Thank. it all worked - Sergey