Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获得';设备未就绪';使用DriveInfo.GetDrives()时出错_C#_.net - Fatal编程技术网

C# 获得';设备未就绪';使用DriveInfo.GetDrives()时出错

C# 获得';设备未就绪';使用DriveInfo.GetDrives()时出错,c#,.net,C#,.net,我试图制作一个程序,查看所有驱动器,试图找到一个名为“domwork”的可移动驱动器,然后获取驱动器名(如E:),然后打印它。很遗憾,我收到了以下错误消息: Unhandled Exception: System.IO.IOException: The device is not ready. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.__Error.Win

我试图制作一个程序,查看所有驱动器,试图找到一个名为“domwork”的可移动驱动器,然后获取驱动器名(如E:),然后打印它。很遗憾,我收到了以下错误消息:

Unhandled Exception: System.IO.IOException: The device is not ready.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.__Error.WinIODriveError(String driveName, Int32 errorCode)
   at System.IO.DriveInfo.get_VolumeLabel()
   at WorkSync.Program.Main(String[] args) in d:\dominic\documents\visual studio 2015\Projects\WorkSync\WorkSync\Program.cs:line 19
我尝试将程序构建为可执行文件,然后以管理员权限运行它,但没有成功

以下是我正在使用的代码:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkSync
{
    class Program
    {
        static void Main(string[] args)
        {
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            string memStickName = "";

            foreach(DriveInfo drive in allDrives)
            {
                Console.WriteLine(drive.VolumeLabel);
                if (drive.VolumeLabel == "DOM WORK" && drive.DriveType.ToString() == "removable")
                {
                    memStickName = drive.Name;
                    break;
                }
            }

            Console.WriteLine(memStickName);
        }
    }
}

您需要确保驱动器实际上可以使用属性访问

foreach(DriveInfo drive in allDrives)
{
  if (drive.IsReady == true)
  {
     // Check volume name here
  }
}