Connecting Tech Pros Worldwide Help | Site Map

Can't delete a file

!NoItAll's Avatar
Member
 
Join Date: May 2006
Location: Madison, Wi
Posts: 76
#1: Jul 22 '09
I'm having a problem deleting a file. I load a TEMPORARY image in a form, show the user the image. They close the form and I attempt to delete the image but get the error that it can not be deleted because it is in use by another process. It seems to me that I'm doing everything right. Here's my code:

Expand|Select|Wrap|Line Numbers
  1. If My.Computer.FileSystem.FileExists(sFileToView.sFileName) Then
  2.         With frmImageViewer
  3.             .LoadImages(sFileToView) = Nothing
  4.             .ShowDialog()
  5.             .Dispose()
  6.             Try
  7.                  My.Computer.FileSystem.DeleteFile(sFileToView.sFileName)
  8.             Catch ex As Exception
  9.                         'silently move on
  10.             End Try
  11.         End With
  12. End If
  13.  
in the form that shows the image this is the code
Expand|Select|Wrap|Line Numbers
  1. Dim Image1 as Bitmap
  2.  
  3. Friend WriteOnly Property LoadImages(ByVal ImageFile As ArcResourceFileType)       
  4.  
  5.         Set(ByVal value)
  6.                 Image1 = Bitmap.FromFile(ImageFile.sFileName)
  7.                 picImage.Image = Image1
  8.         End Set
  9.  
  10. End Property
  11.  
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#2: Jul 22 '09

re: Can't delete a file


That is a known bug in Bitmap.FromFile(string FilePath)

You need to read it into a new bitmap (tempBitmap)
then clone that into a new bitmap (finalBitmap)
then dispose of the tempBitmap
then return the finalBitmap
!NoItAll's Avatar
Member
 
Join Date: May 2006
Location: Madison, Wi
Posts: 76
#3: Jul 22 '09

re: Can't delete a file


I'm still doing something wrong. See the code below - I clone it into another bitmap, then set the original to Nothing - and I still can not delete it!

Expand|Select|Wrap|Line Numbers
  1. Friend WriteOnly Property LoadImages(ByVal ImageFile As ArcResourceFileType)       
  2.  
  3.          Set(ByVal value)
  4.                  Dim TempImage As New Bitmap(ImageFile.sFileName)
  5.                  Dim Image1 as Bitmap = TempImage.Clone()
  6.                  TempImage.Dispose()    'tried it with and without this
  7.                  TempImage = Nothing
  8.                  picImage.Image = Image1
  9.          End Set
  10.  
  11. End Property
  12.  
I get exactly the same error as before - "The process cannot access the file 'C:\Documents and Settings\ddesjardins\Local Settings\Temp\europe.tif' because it is being used by another process."

Arrrrrgh - I'm going nuts over this....
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#4: Jul 22 '09

re: Can't delete a file


Quote:
I clone it into another bitmap, then set the original to Nothing
Quote:
Expand|Select|Wrap|Line Numbers
  1.                  TempImage.Dispose()    'tried it with and without this
  2.                  TempImage = Nothing
  3.  
You try to dispose of it, then try to give the disposed of object a value? Garbage Collection cannot take place on an object this is still in scope and has a value.

Worst case scenario, you might want to stop reading the graphic like that and use a file stream instead. You are welcome to translate my C# code that does this.

Expand|Select|Wrap|Line Numbers
  1.             Image TempImage = null;
  2.             Image FinalImage = null;
  3.             bool Success = false;
  4.  
  5.             if (ImagePath.StartsWith(".")) return FinalImage;
  6.  
  7.             #region Load the image from file
  8.             if (File.Exists(ImagePath))
  9.             {
  10.                 DateTime FailAt = DateTime.Now.AddSeconds(30);
  11.                 while (TempImage == null)
  12.                 {
  13.                     if (DateTime.Now > FailAt) break; // Don't wait forever if the file is junk
  14.                     FileStream fs = null;
  15.                     try
  16.                     {
  17.                         fs = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
  18.                         TempImage = Image.FromStream(fs, false);
  19.                         Success = true;
  20.                     }
  21.                     catch
  22.                     {
  23.                     }
  24.              }
  25.         }
  26.  
This also incorporates multiple attempts to read the file, until a time-out occurs.

Just because windows shows the file, doesn't mean its done writing. Copying and saving can take time.
!NoItAll's Avatar
Member
 
Join Date: May 2006
Location: Madison, Wi
Posts: 76
#5: Jul 22 '09

re: Can't delete a file


You are brilliant sir!
So yes - I did simply change the code to read the image as a filestream, use the FromStream in the Image class, then close the stream. Once closed there was no other contention for the file.
It appears the FromFile method in the Image class is not so good - it holds the file open with no explicit way to close it. That seems like a potential memory leak to me!

Final working code:

Expand|Select|Wrap|Line Numbers
  1. Dim fs As New System.IO.FileStream(sFirstImageFile.sFileName, IO.FileMode.Open, IO.FileAccess.Read)
  2. picLeftImage.Image = Image.FromStream(fs, False)
  3. fs.Close()
  4.  
I'm not bothering with the error checking because I test the file before calling this. Yes - it would be better to make it sensitive to potential errors, but the purpose of this section of code is very limited...
Reply