0

My wpf app creates this temp directory (@"C:\MyAppTemp\"). Inside that directory there are downloaded images. After certain point in app (background workerCompleted) I dont need any more this folder and correspoding files, so I want to deleted this folder, so I tried

if (Directory.Exists(@"C:\MyAppTemp\"))
{
    IOFileUtils.DeleteDirectory(@"C:\MyAppTemp\", true);
    if (!Directory.Exists(@"C:\MyAppTemp\"))
    {
         DisplayMessage = "Temp files deleted";
    }
}

but I'm getting this expcetion {"The process cannot access the file 'C:\MyAppTemp\Client1\6.jpg' because it is being used by another process."}

IOFileUtils.cs
public static void DeleteDirectory(string path, bool recursive)
{
    // Delete all files and sub-folders?
    if (recursive)
    {
        // Yep... Let's do this
        var subfolders = Directory.GetDirectories(path);
        foreach (var s in subfolders)
        {
            DeleteDirectory(s, recursive);
        }
    }

    // Get all files of the folder
    var files = Directory.GetFiles(path);
    foreach (var f in files)
    {
        // Get the attributes of the file
        var attr = File.GetAttributes(f);

        // Is this file marked as 'read-only'?
        if ((attr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
        {
            // Yes... Remove the 'read-only' attribute, then
            File.SetAttributes(f, attr ^ FileAttributes.ReadOnly);
        }

        // Delete the file, RAISES EXCEPTION!!
        File.Delete(f);
    }

    // When we get here, all the files of the folder were
    // already deleted, so we just delete the empty folder
    Directory.Delete(path);
}

UPDATE This code below raises exception

var photosOnTempDir = Directory.GetFiles(dirName);
int imgCounter = 0; //used to create file name
System.Drawing.Image loadedImage;
foreach (var image in photosOnTempDir)
{
    loadedImage = System.Drawing.Image.FromFile(image);
    imageExt = Path.GetExtension(image);
    imgCounter++;
    var convertedImage = Helpers.ImageHelper.ImageToByteArray(loadedImage);
    var img = new MyImage { ImageFile = convertedImage, Name = imgCounter.ToString() };
    myobj.Images.Add(img);
}
user1765862
  • 13,635
  • 28
  • 115
  • 220
  • try adding some delay between `File.SetAttributes` & `File.Delete` method calls. – pushpraj Oct 08 '14 at 07:09
  • 1
    Why so intricately? Directory.Delete(path, true) remove directory and all files and subdirectories. http://msdn.microsoft.com/en-us/library/vstudio/fxeahc5f%28v=vs.100%29.aspx – Valera Scherbakov Oct 08 '14 at 07:13
  • 1
    Why is this question tagged `WPF` when you use `System.Drawing.Image`? That is WinForms. Anyway, the reason why you get this exception is that `System.Drawing.Image.FromFile` leaves the file open. Use `FromStream` instead and close the stream immediately after loading the image, preferably by a `using` block. – Clemens Oct 08 '14 at 08:18

1 Answers1

0

make sure us use "using" with anything that involves those files. you probably hold some kind of a handle that was not disposed yet to one of the files, thus - preventing you deleting it.

Dani
  • 14,639
  • 11
  • 62
  • 110
  • update, how would you decorate updated code to prevent raising an exception. – user1765862 Oct 08 '14 at 07:23
  • You don't "prevent" raising the exception. you can catch it. in order to avoid this specific error you need to dispose all handles to the file you want to delete. – Dani Oct 08 '14 at 07:33