文件正在由另一个进程使用 [英] file in use by another process problem

查看:90
本文介绍了文件正在由另一个进程使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何释放1个窗口中用作背景的照片文件
装配,甚至不再打开.
我试图让用户更改边框上的照片.
当您打开窗口ImageBrush时,它用于Border.Background,

获取图像 图像的绝对文件.当用户更改边框I上的照片
要覆盖该文件.一整天都在努力解决正在使用的文件
由另一个过程exeption.有什么帮助吗?

第2部分
抱歉,我再次将其删除.
尝试总结照片文件是否已用作ImageBrush ImageSource.

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.

推荐答案

发生了什么:某些进程打开文件以进行独占访问(不允许共享),这是默认模式.尝试从您的进程中打开同一文件失败,但会看到异常.此异常的类型为System.IO.IOException.不幸的是,该异常不包含保存文件的进程的句柄.

在保存文件的进程退出之前,无法进行任何操作.您可以终止此过程;这就是您所能做的.

您可以使用System.Diagnostics.Process.GetProcesses枚举所有进程来获取有问题的进程的句柄.使用枚举期间获得的Process实例以某种方式确定您需要的过程.您将获得该流程的实例.使用Process.Kill终止它.

这种方式是有问题的,因为没有用于识别过程的通用标准.您只需要打开文件的过程.您可以使用System.Management进行操作.方法如下:
http://www.pcreview.co.uk/forums/re- get-files-opened-process-t1340888.html [ ^ ].使用此代码示例,您可以找出哪个进程正在保存您的文件以终止该文件.

—SA
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


这篇关于文件正在由另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆