简体   繁体   中英

C# File.Delete, file being used by another process

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


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

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?

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

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.

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.

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;

当所有其他方法都失败时,您可以使用MoveFileEx在下次重新启动时删除该文件。

...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 :@

This problem occurs when trying to delete / modify a previously saved file from the same software. I solved it by adding this code:

System.GC.Collect(); 
System.GC.WaitForPendingFinalizers();


File.Delete(MyFile);

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

Note: Removed link to Unlocker program - contains malware.

I once used something like thestar and Sadegh posted but for some cases it didnt work/helped so I found a diffrent solution I already postet it here

still here the code (u may understand it better after watching the link and the question):

   var imageAsByteArray = File.ReadAllBytes(imagePath);

   // I use as example a pictureBox:
   pictureBox1.Image = byteArrayToImage(imageAsByteArray);

   // Or/and safe/copy/replace it:
   File.WriteAllBytes(picture_Path, imageAsByteArray);

You also can delete the (new) picture instantly ! (if you want)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM