强制删除正在使用的文件 [英] force to delete a file that is in use

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

问题描述

有一个C#formapp。

它创建并使用和删除文件。

删除文件我不知道哪个对象或进程使用它们来处置它们.forcing garbage collection也没有帮助。

如果遇到这个问题你会怎么做?

提前谢谢。

there is a C# formapp .
it creates and uses and delete files.
in deleting files i do not know which object or process is using them to dispose them .forcing garbage collection doesn't help too.
what would you do if you had this problem?
thank you in advance.

推荐答案

要做的第一件事是确保在完成所有与文件相关的对象时始终关闭并处理它们:否则你可能会对文件进行锁定而无法清除它。



如果这不能解决您的问题,那么您需要知道正在使用什么句柄。

这很复杂到在代码中做,但Microsoft已经为您完成了工作:只需在进程中运行Handle.Exe并解析结果。 http://technet.microsoft.com/en-gb/sysinternals/bb896655.aspx [ ^ ] - 这是一种笨拙的方法,但它应该工作(您可能需要进程提升才能使用它)。

完成此过程后,您可以通知正在使用该文件的用户,并让他保存他的工作并关闭处理 - 我强烈建议你不要自己开始查杀进程,因为大多数进程因为使用它而打开文件,如果你在用户有机会之前杀死进程,几乎肯定会丢失数据并惹恼用户save ...
The first thing to do is to ensure that you always close and dispose all file related objects when you are finished with them: otherwise you will likely leave a lock on the file and won't be able to clear it.

If that doesn't solve your problem, then you need to know what handles are in use.
That's complicated to do in code, but Microsoft has done the work for you: just run Handle.Exe in a process and parse the results. http://technet.microsoft.com/en-gb/sysinternals/bb896655.aspx[^] - It's a clumsy looking approach, but it should work (you may need process elevation to use it).
Once you have the process, you can inform the user who is using the file, and get him to save his work and close the handle - I would strongly suggest that you don;t start killing processes yourself, given that most processes have the file open because they are using it, and will almost certainly lose data and annoy users if you kill processes before the user gets a chance to save...


如果正在使用文件,则无法强制删除它。这是大多数现代操作系统非常重要的保护功能。没有它,系统就无法以稳定的方式运行。您只能终止持有该文件的进程,然后才删除它。但是不要这样做。您需要进行适当的清理,并确保进程在不使用时关闭文件(或者恰恰相反,保留它们以防止删除它们,以及可选的修改)。



现在,问题是:什么过程保存有问题的文件?这很容易调查。



首先,类似的问题在这里被多次询问,根据我的经验我知道:在大多数情况下,阻止过程是你的自己的过程。您可能忘记在同一个应用程序中处理/关闭某些内容。所以,首先,检查一下。要探索这种可能性,请参阅我过去的答案:

清除C#中的句柄 [ ^ ]。



在这个答案中,注意使用 / code>语句可以帮助您保证在使用后正确处理相应的文件系统对象,而不是保持文件锁定。



在某些情况下,你真的需要调查哪个进程拥有哪个文件。为此,我建议使用 Sysinternals Suite 中的一个实用程序。这组实用程序(以前来自 Winternals 公司,目前在Microsoft)是必须的对于任何开发人员,请参阅:

http://technet.microsoft.com/en -us / sysinternals / bb842062 [ ^ ],

http://technet.microsoft.com/en-us/sysinternals / bb545027 [ ^ ]。



您需要的实用程序是 handle.exe ,请参阅:

http://technet.microsoft.com/en-us/sysinternals/bb896655 [ ^ ]。



在您的情况下,您使用文件名参数:

If a file is in use, you cannot enforce deleting it. This is the very important protective feature of most modern OS. Without it, the system could not behave in a stable way. You can only kill the process holding the file and only then remove it. But don't do it. You need to do proper clean-up instead and make sure the processes close the files when they are not using (or, just the opposite, hold them to prevent deleting them and, optionally, the modifications).

Now, the question is: what process holds the file in question? This is easy to investigate.

First of all, the similar question was asked here many times, and from this experience I know: in most cases the blocking process is your own process. You could have forgotten to dispose/close something in the same application. So, first of all, check it up. To explore this possibility, please see my past answer:
Clearing a Handle in C#[^].

In this answer, pay attention for the using of the using statement which helps you to guarantee that appropriate file system object is properly disposed after use, not keeping the file locked.

In some cases, you really need to investigate which process holds which file. For this, I recommend using one utility from the Sysinternals Suite. This set of utilities (formerly from Winternals company, presently at Microsoft) is a must-have for any developer, please see:
http://technet.microsoft.com/en-us/sysinternals/bb842062[^],
http://technet.microsoft.com/en-us/sysinternals/bb545027[^].

The utility you need is "handle.exe", please see:
http://technet.microsoft.com/en-us/sysinternals/bb896655[^].

In your case, you use it with file name parameter:
handle.exe <file_name>





此实用程序将扫描所有类型的句柄,而不仅仅是文件句柄。对于文件,它将扫描与文件名匹配的所有文件句柄(因此它不必是完整路径名)并返回足以识别每个进程的信息,包括其 pid 。因此,如果您需要有关相关流程的更多信息,您还可以使用其他Sysinternals实用程序,特别是其 Process Explorer

http://technet.microsoft.com/en-us/sysinternals/bb896653 [ ^ ]。



祝你好运,

-SA



This utility will scan all kinds of handles, not just file handles. For file, it will scan all file handles matching the file name (so it does not have to be a full path name) and return information sufficient to identify each process, including its pid. So, if you need more information on a process in question, you can also use other Sysinternals utilities, in particular, its Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653[^].

Good luck,

—SA


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

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