检测文件是否正在使用中 [英] Detecting if a File is in Use

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

问题描述

我有一个C#(.NET 1.1)应用程序,我在其中循环了硬盘驱动器上的许多文件并删除它们。有几次,我遇到了一个异常

,说该文件正被另一个进程使用。


所以,我的问题是......无论如何都要循环文件并确定

如果在尝试删除之前能够删除文件(那么

异常不会被抛出)?


谢谢。


I have a C# (.NET 1.1) application in which I loop over a number of files on
the hard drive and delete them. A few times, I''ve experienced an exception
that says the file is in use by another process.

So, my question is... Is there anyway to loop over the files and determine
if the files are able to be deleted before trying to delete (so the
exception won''t be thrown)?

Thanks.


推荐答案



ABN <无**** @ company.com>在消息中写道

news:eu ************** @ TK2MSFTNGP11.phx.gbl ...

"ABN" <no****@company.com> wrote in message
news:eu**************@TK2MSFTNGP11.phx.gbl...
我有一个C#( .NET 1.1)应用程序,我在硬盘上循环了许多文件并删除它们。有几次,我遇到了一个
异常
,说该文件正在被另一个进程使用。

所以,我的问题是......无论如何都有循环遍历文件并确定
是否可以在尝试删除之前删除文件(因此不会抛出
异常)?
I have a C# (.NET 1.1) application in which I loop over a number of files
on
the hard drive and delete them. A few times, I''ve experienced an
exception
that says the file is in use by another process.

So, my question is... Is there anyway to loop over the files and
determine
if the files are able to be deleted before trying to delete (so the
exception won''t be thrown)?




这样做是否有一个很大的优势,只需捕获异常?

(并且无法保证当你使用时没有使用的文件检查

当你尝试删除它时不会被使用,所以无论如何你都需要处理

异常。)



Is there a big advantage to doing this over simply catching the exception?
(And there''s no guarantee that a file which wasn''t in use when you checked
won''t be in use when you then try to delete it, so you''d need to deal with
the exception anyway.)


你好ABN,


你需要使用WINAPI(interops调用)来遍历这些进程

并找到哪一个持有你的文件处理程序。

但是使用异常比P / Invoke更自然


A>我有一个C#(.NET 1.1)应用程序,我在其中循环了一些

A>硬盘上的文件并删除它们。几次,我已经

A>经历了一个异常,说该文件正被另一个人使用

A>过程。

A>

A>所以,我的问题是......无论如何都要循环文件和

A>确定在尝试删除之前是否可以删除文件

A> (所以不会抛出异常)?

A>

A>谢谢。

A>

---

WBR,

Michael Nemtsev ::博客: http://spaces.msn.com/laflour


有时一个人仍然忠于一个事业,只因为它的对手没有,b $ b不再是平淡无奇的。 (c)Friedrich Nietzsche
Hello ABN,

You need to use WINAPI for this (interops call) to iterate through the processes
and find which one is holding your file handler.
But using exception is more natural way than P/Invoke

A> I have a C# (.NET 1.1) application in which I loop over a number of
A> files on the hard drive and delete them. A few times, I''ve
A> experienced an exception that says the file is in use by another
A> process.
A>
A> So, my question is... Is there anyway to loop over the files and
A> determine if the files are able to be deleted before trying to delete
A> (so the exception won''t be thrown)?
A>
A> Thanks.
A>
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


同意。


此外,您无法保证用户拥有管理权

有权迭代这些流程。


要求管理员是不合理的。这个理由仅仅为了这个原因而运行该程序的权利。


Michael Nemtsev < ne ***** @ msn.com>

???????:9c ******************** ****** @ msnews.micros oft.com ...
Agreed.

Additionally, you have no guarantee that the user have the administrative
right to iterate through the processes.

It''s not justifable to require admin. right to run the program solely for
this reason.

"Michael Nemtsev" <ne*****@msn.com>
???????:9c**************************@msnews.micros oft.com...
你好ABN,

你需要使用WINAPI(interops call)来迭代
处理并找到哪一个持有你的文件处理程序。
但是使用异常比P / Invoke更自然
Hello ABN,

You need to use WINAPI for this (interops call) to iterate through the
processes and find which one is holding your file handler.
But using exception is more natural way than P/Invoke






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

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