How to delete files or folders in use? C #

0

I would like to know the way in which my folders should be deleted within my C # application, the images that are inside my folders are in use, not all of them only, so I would like to know how to remove that use or close the image before deleting so that when you get to the command you can do the deletion correctly.

Current Code:

    string directorio = @"..\..\Imagenes\Imagenes_Modal\Usuario\Abril";



    if (Directory.Exists(directorio)) {
        Directory.Delete(directorio,true);
    }

The error I get is the following:

  

The process can not access the file 'Abril0.jpeg' because   is being used in another process.

    
asked by David 05.04.2017 в 23:50
source

1 answer

3

To be able to delete a file you must work it without it being blocked by the application, for example if the idea is to show this in a Picturebox you would use something like being

using (var stream = File.Open(imageFilePath, FileMode.Open))
{
   PictureBox1.Image = Image.FromStream(stream);
}

The idea is that you can destroy the object that accesses the file so that it is not locked.

I do not know what operation you do with the file, but you should analyze the code that you implemented and be able to enter the using so that after reading the file is released and you can delete it.

    
answered by 06.04.2017 / 02:10
source