views:

7108

answers:

10

Hey,

I have a problem when I'm trying to delete an image file. I always get an error that says: IOExeption was unhandled. Acces denied because the file is beining used by another process.

I do'nt know what process that could be and how to solve it.

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {            
            Album album = GetAlbum(comboBox1.SelectedIndex);
            Photo photo = GetPhoto(comboBox1.SelectedIndex, comboBox3.SelectedIndex);           

            txtPhotoPath.Text = Directory.GetCurrentDirectory() + "\\"  + photo.SPath;

            lblExtention.Text = photo.SExtention;
            txtPhotoTitle.Text = photo.STitle;
            pctrbFoto.Image = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr());
        }

private void btnChangePhoto_Click(object sender, EventArgs e)
{
            Album album = GetAlbum(comboBox1.SelectedIndex);
            Photo photo = GetPhoto(comboBox1.SelectedIndex, comboBox3.SelectedIndex);

            File.Delete("Albums\\Images\\" + photo.STitle + foto.SExtention);

            photo.SExtention = lblExtention.Text;
            photo.STitle = txtPhotoTitel.Text;
            Photo.SPath = txtPath.Text;

            File.Copy(photo.SPath, "Albums\\Images\\" + photo.STitle + photo.SExtention);

}

Thanks, Vinzcent

+5  A: 

The first area I would look is in your GetPhoto method. Do you have a StreamReader that hasn't been closed? Make sure that if you're doing any sort of I/O on the file prior to the delete that you close those connections first. What does the GetPhoto() method do?

Scott Anderson
+3  A: 

First you need to determine if it is your application or another application that has the file open.

You can use Process Explorer by Mark Russinovich to see which program has a particular file or directory open. It is part of the Windows Sysinternals line of excellent utilities that every programmer/IT professional should use (or at least be aware of).

You can get it here: http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

Robert Cartaino
A: 

you can use the Unlocker program to tell you what program(s) have the file locked

Note: Removed link to Unlocker program - contains malware.

Rossini
You can also use Process Explorer to tell you what program(s) have a file locked ( http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx )
Brian
+2  A: 

When you call Image.FromFile in comboBox3_SelectedIndexChanged, and perhaps elsewhere as well, you don't dispose the Image object. Therefore, your program is keeping the file in use.

You need to Dispose the image every time you open it.

SLaks
+1  A: 

When all else fails, you can use MoveFileEx to delete the file on next reboot.

Sii
+3  A: 

where you are getting the thumbnail use:

using(Image img = Image.FromFile(foto.SPath))
{
  pctrbPhoto. Image = img.GetThumbnailImage(
  GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), 
  GetfHeight(photo.SPath, 150), null, new IntPtr()); 
}

instead to ensure that the source image is disposed (closed) when you are finished with it.

They way you have it, the image that is loaded from the file sticks around until the garbage collector decides to release it, which might be some time.

Images loaded with FromFile hold the file that they were loaded from open.

thestar
+1  A: 

your process is the one that uses file , you need to set image to null use something like this :

using(var img = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr()))
  pctrbFoto.Image = img;
Adinochestva
In general, it's a bad idea to call GC.Collect: http://blogs.msdn.com/ricom/archive/2004/11/29/271829.aspxSince Image implements IDisposable, you should call img.Dispose() instead, or (preferably) use a "using" block.
Bradley Grainger
yes u r right ;)
Adinochestva
A: 

Thanks to all for the help.

I used this and it works very well now


your process is the one that uses file , you need to set image to null use something like this :

var img = Image.FromFile(foto.SPath).GetThumbnailImage(GetWitdth(photo.SPath, GetHeight(photo.SPath, 150)), GetfHeight(photo.SPath, 150), null, new IntPtr());

pctrbFoto.Image = img;

img = null;

GC.Collect();

Vinzcent
A: 

...but, if your application is running on a web hosting plan? You can't run any software in a shared server.

I've tried with dispose() and other options, but I can't delete files like Vinzcent.

Maldito IIS :@

A: 

Just as a note, the Unlocker software doesn't contain malware...

JT24706