Can't delete file (process cannot access the file because it is being used by another process)
<p>I try to delete files from a directory I specify. The first time it works great, but the second time it gives the next exception:</p>

<blockquote>
  <p>The process cannot access the file 'C:\Users\DAAGEU\AppData\Local\Temp\Manual_Avatar\AdamBody_1K_Pants_Black_Blended.jpg' because it is being used by another process.</p>
</blockquote>

<pre><code>foreach (String file in Directory.GetFiles((userDirectory)))
{
    try
    {
        //using (FileStream fs = new FileStream(file, FileMode.OpenOrCreate))
        //{
            //fs.Unlock(fs.Position, fs.Length);
            //fs.Dispose();
            //fs.Close();
        //}

        File.Delete(file);
    }
    catch (Exception)
    {
        throw;
    }
}
</code></pre>

<p>It are only the images that seems to be "locked". I found some stuff on stackoverflow but nothing helped? I use some 3rd party software to generate the image.</p>

<p>Any 1 knows how to fix this or work around it??</p>

<p>ANSWER:</p>

<p><strong>I found the problem. I was using WinForms and in one WinForm I was hosting a WPF control with the image. I found out that I didn't dispose that UC after closing it. So my fault I should have mensioned that also in this question...</strong></p>
