Click here to Skip to main content
15,881,801 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I would like to know how to release a photo file used as background by 1 window in
the assembly, that isnt even open anymore.
I am trying to let the user change the photo on a Border.
when you open the window the ImageBrush, used for the Border.Background,
get an Image from a
absolute file for the image. when the user changes the photo on the border I
want to overWrite that file . tried all day to work around the File in use
by another process exeption. any help ?

PART 2
sorry I deleted it here it is again.
to try to summarize if a photo file is in use as a ImageBrush ImageSource.
how best to overwite that file with a new image.
Posted
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 21:43pm    
See my restored Answer.
--SA
o1010wanabe 12-Apr-11 21:43pm    
sorry SAKryukov I re posted it , I wasnt confident about whether my question was spcecific or simple enough.
Sergey Alexandrovich Kryukov 12-Apr-11 21:46pm    
No problem.
Well, the question is simple enough, but not the easiest to answer, so I'll vote 5 for your Question.
Let me know if it can work for you -- some more problems may come up.
--SA

1 solution

What happens: some process opens the file for exclusive access (sharing not allowed) which is the default mode. An attempt to open the same file from your process fails with the exception you observe. The type of this exception is System.IO.IOException. Unfortunately, the exception does not contain the handle of the process holding the file.

Nothing can be done until the process holding the file exits. You can terminate this process; this is all you can do.

You can get the handle of the offending process by enumerating all the processes using System.Diagnostics.Process.GetProcesses; somehow identify the process you need using the instance of the Process obtained during enumeration. You will get the instance of the process. Use Process.Kill to terminate it.

This way is problematic because there is no a universal criterion used to identify the process. You need only the process which has opened your file. You can do it using System.Management. Here is how:
http://www.pcreview.co.uk/forums/re-get-files-opened-process-t1340888.html[^]. Using this code sample you can find out which process is holding your file to terminate it.

—SA
 
Share this answer
 
Comments
Toli Cuturicu 13-Apr-11 3:27am    
Wow!
Sergey Alexandrovich Kryukov 13-Apr-11 4:12am    
Aha!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900