DriveInfo.DriveFormat上的设备未就绪错误

R.V*_*tor 1 c# driveinfo

我有那个方法来检索NTFS的可移动设备信息:

    private void getdriverinfo()
    {
        // get the usb flash driver
        foreach (DriveInfo driveInfo in DriveInfo.GetDrives())
        {
            if (driveInfo.DriveType == DriveType.Removable && driveInfo.DriveFormat.Equals("NTFS"))
            {
                comboBox1.Items.Add(driveInfo.Name);
            }
        }
        if (comboBox1.Items.Count == 0)
        {
            MessageBox.Show("No Removable Device Found , please plug in the USB drive and make sure it is in NTFS format and retry", "No Device Found!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else if (comboBox1.Items.Count == 1)
        {
            comboBox1.Text = comboBox1.Items[0].ToString();
        }
        else
        {
            MessageBox.Show(comboBox1.Items.Count + " Removable Devices were found , please choose the device you want to protect");
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // get the usb flash driver
        getdriverinfo();       
    }
Run Code Online (Sandbox Code Playgroud)

发生此错误:

System.IO.IOException:设备尚未就绪.

在System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)

在System.IO .__ Error.WinIODriveError(String driveName,Int32 errorCode)

在System.IO.DriveInfo.get_DriveFormat()

在USB_Data_Protector.Form1.getdriverinfo()

这在我的笔记本电脑上工作正常,没有错误.当它在虚拟PC或其他PC上运行时,会显示此错误.

cha*_*dmk 5

您可以在访问DriveFormat之前检查以下内容吗? IsReady Property

driveInfo.IsReady
Run Code Online (Sandbox Code Playgroud)