DriveInfo.GetDrives使用错误备忘

来源:互联网 发布:windows live安装出错 编辑:程序博客网 时间:2024/06/05 20:51
DriveInfo[] allDrives = DriveInfo.GetDrives();foreach (DriveInfo info in allDrives){    if (info.DriveFormat == "FAT")        {            //Do something...        }}


错误提示

未处理的异常:  System.IO.IOException: 设备未就绪。
   在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   在 System.IO.__Error.WinIODriveError(String driveName, Int32 errorCode)
   在 System.IO.DriveInfo.get_DriveFormat()
   在 DriveInfoHelper.Program.Main(String[] args)


改正,访问设备的属性前要判断设备是否就绪

DriveInfo[] allDrives = DriveInfo.GetDrives();foreach (DriveInfo info in allDrives){    if (info.IsReady        && info.DriveFormat == "FAT")        {            //Do something...        }}


0 0