如何删除这是由C#另一个进程锁定的文件? [英] How do I delete a file which is locked by another process in C#?

查看:319
本文介绍了如何删除这是由C#另一个进程锁定的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来删除它是用C#另一个进程锁定的文件。我怀疑该方法必须能够找到哪个进程锁定该文件(可能通过跟踪手柄,虽然我不知道如何在C#中做到这一点),然后能够完成文件删除使用<$之前关闭此进程C $ C> File.Delete()。

I'm looking for a way to delete a file which is locked by another process using C#. I suspect the method must be able to find which process is locking the file (perhaps by tracking the handles, although I'm not sure how to do this in C#) then close that process before being able to complete the file delete using File.Delete().

推荐答案

中止其他进程是不是一个健康的事。如果你的方案涉及类似卸载,你可以使用<$c$c>MoveFileEx API函数的在下次重启删除文件标记。

Killing other processes is not a healthy thing to do. If your scenario involves something like uninstallation, you could use the MoveFileEx API function to mark the file for deletion upon next reboot.

如果您似乎真的需要另一个进程删除使用中的文件,我建议你再考虑考虑任何解决方案之前的实际问题。

If it appears that you really need to delete a file in use by another process, I'd recommend re-considering the actual problem before considering any solutions.

这篇关于如何删除这是由C#另一个进程锁定的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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