简体   繁体   English

是什么导致DriveInfo.IsReady为假?

[英]What causes DriveInfo.IsReady to be false?

Situation 情况

I'm debugging some legacy code that performs some existence checks on directory paths. 我正在调试一些在目录路径上执行某些存在检查的遗留代码。 First, DirectoryPathA is checked and returned if it exists. 首先,检查并返回DirectoryPathA (如果存在)。 This should be the usual case. 这应该是通常的情况。 If that fails, DirectoryPathB is checked and returned. 如果失败,则检查并返回DirectoryPathB (If that fails too, other things happen, not part of this question). (如果那也失败了,其他事情就会发生,而不是这个问题的一部分)。 Here's how the code looks: 以下是代码的外观:

if (!string.IsNullOrEmpty(DirectoryPathA))
{
    driveInfo = new DriveInfo(DirectoryPathA);
    if (driveInfo.IsReady)
    {
        dInf = new DirectoryInfo(DirectoryPathA);
        if (dInf.Exists)
        {
            return DirectoryPathA;
        }
    }
}

if (!string.IsNullOrEmpty(DirectoryPathB))
{
    dInf = new DirectoryInfo(DirectoryPathB);
    if (dInf.Exists)
    {
        return DirectoryPathB;
    }
}

As you can see, the first check has an additional guard based on DriveInfo.IsReady . 如您所见,第一次检查有一个基于DriveInfo.IsReady的额外保护。 There have been problems with file access on the network before, so my assumption is (again, this is legacy code) that this was introduced to establish DirectoryPathB as an alternative if DirectoryPathA is not available. 以前在网络上存在文件访问问题,所以我的假设是(再次,这是遗留代码),如果DirectoryPathA不可用,则会引入这个来建立DirectoryPathB作为替代方案。 I have no idea why no such check for DriveInfo.IsReady is performed on DirectoryPathB . 我不知道为什么在DirectoryPathB上没有执行对DriveInfo.IsReady检查。

Problem 问题

Even if DirectoryPathA is a local directory (so no network outage), the code above sporadically returns DirectoryPathB . 即使DirectoryPathA是本地目录(因此没有网络中断),上面的代码偶尔会返回DirectoryPathB The code is executed several times and is assumed to be idempotent, which it is not, which breaks things. 代码执行了几次,并且被认为是幂等的,它不是,它会破坏事物。 This only happens occasionally on some machines over the course of several hours. 这只会在几个小时内偶尔发生在某些机器上。 I cannot reproduce the problem. 我无法重现这个问题。

There's a fundamental problem with the assumption that the drive state would never change throughout the execution of a program. 假设在整个程序执行期间驱动器状态永远不会改变,这是一个基本问题。 I understand that. 我明白那个。 However, this code seems to be fine most of the time and has been in the past. 但是,这段代码在大多数情况下似乎都很好并且已经过去了。

Question

What can cause DriveInfo.IsReady to be false ? 什么可能导致DriveInfo.IsReadyfalse

The documentation states 文件说明

true if the drive is ready; 如果驱动器准备就绪,则为true ; false if the drive is not ready. 如果驱动器未准备好,则为false

Thanks for nothing. 什么都不感谢。 The remarks section adds 备注部分补充说

IsReady indicates whether a drive is ready. IsReady指示驱动器是否准备就绪。 For example, it indicates whether a CD is in a CD drive or whether a removable storage device is ready for read/write operations. 例如,它指示CD是否在CD驱动器中,或者可移动存储设备是否已准备好进行读/写操作。 If you do not test whether a drive is ready, and it is not ready, querying the drive using DriveInfo will raise an IOException. 如果您未测试驱动器是否准备就绪,并且尚未就绪,则使用DriveInfo查询驱动器将引发IOException。

  • It's not a removable drive. 它不是可移动驱动器。 We're talking plain old C:\\ . 我们说的是普通老C:\\
  • It's not a permission issue because it sometimes works and sometimes doesn't for the same user. 这不是权限问题,因为它有时可以工作,有时不适用于同一个用户。
  • It's not a matter of the drive becoming unavailable due to power saving mode, because 这不是驱动器由于省电模式而变得不可用的问题,因为
    • power saving mode is not active and 省电模式未激活
    • it's an SSD. 这是一个SSD。

The old documentation states that 旧文件说明了这一点

Thread Safety 线程安全

Any public static ( Shared in Visual Basic) members of this type are thread safe. 此类型的任何公共静态(在Visual Basic中为Shared)成员都是线程安全的。 Any instance members are not guaranteed to be thread safe. 任何实例成员都不保证是线程安全的。

Does that mean that DriveInfo.IsReady might be false if some other thread is performing some operation on the drive at the same time? 这是否意味着如果某个其他线程同时在驱动器上执行某些操作, DriveInfo.IsReady可能为false Is it even necessary for DriveInfo.IsReady to be true in order to check if a directory exists? 它甚至有必要DriveInfo.IsReadytrue ,以检查是否存在一个目录?

I met a situation that Ntfs file system corrupted and event 55 of Ntfs triggered. 我遇到了Ntfs文件系统损坏和Ntfs事件55触发的情况。 At this time DriveInfo.isReady() returns false. 此时DriveInfo.isReady()返回false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM