PaulDev

Hi there,

I am trying to delete image files from static collection of images and every time It throws an IO Exception {"The process cannot access the file D:\... because it is being used by another process."}. This is the sample of my code.

Here the ThumbnailFactory is the static class with a collection of images ThumbnailInfo.

// Local collection to hold the list of files to be deleted
List
<string> lstRemove = new List<string>();

foreach
(ListViewItem lstItem in this.etListView.SelectedItems)
{
  FileInfo fileInfo = lstItem.Tag as FileInfo;
 
this.AssetListView.Items.Remove(this.AssetListView.SelectedItems[0]);
  foreach (ThumbnailFactory.ThumbnailInfo ti in ThumbnailFactory.ActiveThumbnails)
      if (ti.Path == fileInfo.FullName)
      {
        
ThumbnailFactory.ActiveThumbnails.Remove(ti);
        
break;
     
}
    
lstRemove.Add(fileInfo.FullName);  
}

etListView.SelectedItems.Clear();

foreach
(string fName in lstRemove)
     System.IO.File.Delete(fName);

The Error occurs in the last line when the method "System.IO.File.Delete(fName);" is called. Strangle it happens only when the file type is an image.

When I ran sysinternals "handle" utility to see which process was holding refence to the file it is listed under my application exe in Section       \BaseNamedObjects\netfxcustomperfcounters.1.0.net clr networking .... any Bells !!

Would be glad if some one could help me !
- Paul



Re: Visual C# General IO Exception when Deleteing a File

Khin

hi paul,

it is not only happen to image file type, i faced this before also.

how you collect the ThumbnailInfo you read from the image

if like this the program that you run are using the image, so a file which are using cannot be remove/delete.

because our program are running too fast so even the program finish reading that image, in memory that image haven't be released.

so maybe u can try to "Sleep()" a while.

that how i solve this problem :) maybe not so profesional...but it work without any error... :)





Re: Visual C# General IO Exception when Deleteing a File

micvos

What is thumbnailinfo Does it hold an image, because if it does you have to dispose of it to release the file handles. Otherwise you'd have to wait for the garbage collector to do it for you but you never now when this is gonna happen.




Re: Visual C# General IO Exception when Deleteing a File

PaulDev

Hi Khin...

Thanks for the tip. I tried the sleep trick ... but it did not work. I made the code run in debug mode and it waited a min before I let the delete execute. .

- Paul





Re: Visual C# General IO Exception when Deleteing a File

PaulDev

Hi Micvos,

Thanks for the reply. thumbnailinfo is a structure. It has three image properties and a couple of strings. It holds reference to that image object created from the image file. And as far as I know we cannot have distructors for structures.

Just to be sure that the object gets cleared I added the following code and it still would not work.

if (ti.Path == fileInfo.FullName)
{
ThumbnailFactory.ActiveThumbnails.Remove(ti);
ti.OriginalImage =
null;
ti.Thumbnail =
null;
ti.ThumbnailBigIcon = null;
break;
}

- Paul





Re: Visual C# General IO Exception when Deleteing a File

PaulDev

Hi Guys,

I solved the issue looking at post in a different forum. The reason for not being able to delete the file is Image object is holding reference to the file event after it has been set as null. Had to call the Dispose() method of the image object of the. Looks like it is due to some GDI+ issue. The posting that I looked into is here specially note what the user nobugz  says about the image calss.

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=547070&SiteID=1

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=656321&SiteID=1

and I changed my code above as this

if (ti.Path == fileInfo.FullName)
{
 
ThumbnailFactory.ActiveThumbnails.Remove(ti);
 
  if (ti.OriginalImage != null)
    ti.OriginalImage.Dispose();
  
  if (ti.Thumbnail !=
null);
    ti.Thumbnail.Dispose();

  if (
ti.ThumbnailBigIcon != null);
    ti.ThumbnailBigIcon.Dispose();
 
break;
}

Thanks so much guys for your help.

- Paul