有没有办法立即检查目录是否在使用? [英] Is there a way to instantly check whether a directory is in use?

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

问题描述

我想用Directory.Move移动一个目录及其所有的子目录。然而,在我这样做之前,我想检查目录及其子目录中的任何文件和子文件是否正在被其他进程使用。

然后,在移动之前,我想将目录锁定到其他进程,所以我可以确定Directory.Move不会抛出任何异常。

/ p>

最好的方法是什么?

我想避免检查个人的使用情况文件的文件,因为文件没有被使用时,软件检查它并不意味着它不会被用于移动过程开始。

无法锁定文件夹(+子文件夹),所以你总是会以竞赛条件,并不保证不会有任何例外。



总有这样的可能性,

试着移动文件夹,如果不成功,稍后再试。



参见al所以:德尔福:检查文件是否在使用中(这是一个类似的问题,只是忽略了Delphi部分)

I want to move a directory and all of its subdirectories with Directory.Move.

Before I do that, however, I want to check whether any of the files and subfiles in the directory and its subdirectories are being used by other processes.

Then, before the move, I'd like to lock the directory to other processes, so I can be sure that Directory.Move won't throw any exceptions.

What is the best way to accomplish that?

I would like to avoid checking for the usage of the individual files file by file, because the fact that the file isn't used when the software checks for it does not mean that it won't be used when the movement process starts.

解决方案

There is no way to lock the folder (+ sub folders) so you'll always end up with a race condition and there is no guarantee that there will be no exception.

There is always the possibility that something changes in between the check and the move so things can go wrong.

Just try to move the folder and retry later if not succeeding.

See also: Delphi: Check whether file is in use (this is a similar question, just ignore the Delphi part)

这篇关于有没有办法立即检查目录是否在使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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