我正在尝试创建一个查看所有驱动器的程序,尝试找到名为“DOM WORK”的可移动程序,然后获取驱动器名称(如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);
}
}
}
答案 0 :(得分:5)
您需要确保使用DriveInfo.IsReady属性实际访问该驱动器。
foreach(DriveInfo drive in allDrives)
{
if (drive.IsReady == true)
{
// Check volume name here
}
}