473,513 Members | 8,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't delete a file

!NoItAll
297 Contributor
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.  
Jul 22 '09 #1
4 2451
tlhintoq
3,525 Recognized Expert Specialist
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
Jul 22 '09 #2
!NoItAll
297 Contributor
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....
Jul 22 '09 #3
tlhintoq
3,525 Recognized Expert Specialist
I clone it into another bitmap, then set the original to Nothing
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.
Jul 22 '09 #4
!NoItAll
297 Contributor
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...
Jul 22 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3187
by: lawrence | last post by:
I've two scripts, one to upload images, another to delete them. The upload script works fine, but the delete script has permissions trouble. How can PHP not have permission to delete an image it...
18
9072
by: Dino | last post by:
dear all, i've created an application for a customer where the customer can upload ..csv-files into a specified ftp-directory. on the server, a php-script, triggered by a cronjob, reads all the...
0
1648
by: Phil... | last post by:
I have a directory with a lot of jpg files. I want to delete the small ones. Small means less than 100 pixels by 100 pixels. I use File.list() to get the list of files. I use ImageIcon(filename)...
0
2078
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape....
5
5213
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
12
7969
by: Lucas Tam | last post by:
I have a very simple loop: If (Directory.Exists(tempDirectory)) Then Try Dim Files() As String = Directory.GetFiles(tempDirectory) 'Clear out directory For Each Filename As String In Files...
9
8307
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3)...
2
4727
by: createdbyx | last post by:
I am trying to make a file sync utillity to sync files between my laptop and my desktop pc. On my desktop machine (xp pro sp2) I have shared my "Visual Studio Projects" folder using windows simple...
2
2735
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
13
2371
by: darrel | last post by:
When I write scripts for deleting records in databases, I tend to do it in this fashion: - delete link passes ID to a delete.aspx page - delete.aspx page queries db using that ID to retrieve the...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5079
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.