Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 309
Vinzcent Avatar asked May 01 '09 14:05

Vinzcent


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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?

like image 64
Scott Anderson Avatar answered Oct 18 '22 04:10

Scott Anderson


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.

like image 27
thestar Avatar answered Oct 18 '22 05:10

thestar