Why is the file used by another process?
-
My app loads an image using Image.FromFile() and after some processing the image is saved in another file and the Image object is disposed. But when I need to delete original file, an exception box appears. The problem is that my app (process) is still using original image file. How to "unmark" the original file from "used" state back to normal "free to write/delete" state?
-
My app loads an image using Image.FromFile() and after some processing the image is saved in another file and the Image object is disposed. But when I need to delete original file, an exception box appears. The problem is that my app (process) is still using original image file. How to "unmark" the original file from "used" state back to normal "free to write/delete" state?
Is your app process still running after you close/exit your app? If so, that's strange and means it was not disposed. If not, then it's most likely because your Visual Studio is open and is using it. Try running your app, with Visual Studio not running and see if that was the problem.
-
My app loads an image using Image.FromFile() and after some processing the image is saved in another file and the Image object is disposed. But when I need to delete original file, an exception box appears. The problem is that my app (process) is still using original image file. How to "unmark" the original file from "used" state back to normal "free to write/delete" state?
The process that is keeping the file open is yours. When you use FromFile to load an image, that file is kept open for the lifetime of the Image object (don't ask me why!) The work around is to open the file as a stream, the create the Image from that stream, then close the file.
// The using statement will automatically Close and Dispose the
// FileStream when you're done using it
using(FileStream fs = new FileStream(fileName, FileMode.Open))
{
PictureBox1.Image = Image.FromStream(fs);
}RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
The process that is keeping the file open is yours. When you use FromFile to load an image, that file is kept open for the lifetime of the Image object (don't ask me why!) The work around is to open the file as a stream, the create the Image from that stream, then close the file.
// The using statement will automatically Close and Dispose the
// FileStream when you're done using it
using(FileStream fs = new FileStream(fileName, FileMode.Open))
{
PictureBox1.Image = Image.FromStream(fs);
}RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Thanks a lot. Now it works fine, damn Image.FromFile()...