C # File.Delete, file used by another process

I have a problem when I try to delete an image file. I always get an error: IOExeption was unhandled. Acces denied because the file is used for another process.

I do not know what process 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


Thank you all for your help.

I used this and it works fine now


- , , null, - :

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

pctrbFoto.Image = img;

img = null;

GC.Collect();

+5
8

- , , null - :

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;
+2

, , GetPhoto. StreamReader, ? , - - , . GetPhoto()?

+6

, .

Process Explorer , , . Windows Sysinternals , /- (, , ).

: http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

+3

:

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()); 
}

, (), .

, , , , , .

, FromFile, , .

+3

Image.FromFile comboBox3_SelectedIndexChanged , , , Image. .

.

+2
+1

Unlocker, ,

Note. Remote link to the Unlocker program - contains malware.

0
source

... but if your application works in terms of web hosting? You cannot run any software on a shared server.

I tried with dispose () and other parameters, but I cannot delete files like Vinzcent.

Maldito IIS: @

0
source

All Articles