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:
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.
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;
}
}
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.
Any 1 knows how to fix this or work around it??
ANSWER:
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…
Try to delete your file the usual way through windows explorer. If you get that annoying popup there saying Could not delete… It is being used by another person or program you can download Unlocker and have it tell you exactly which process has a lock on it. This little program has been very useful to me throughout the years. It even runs from a memorystick, so installing is an option. Installing it is definitely a plus though since it will automatically pop up with the name of the locking program when you get that popup.