日期:2014-05-19  浏览次数:20964 次

*******c#判断软驱是否存在??********急!!
我想遍历本机所有磁盘,但是有一种情况出错:也就是BIOS中开在软驱(A:/B:),实际并没有安装软驱,在系统中也显示有软驱盘符。运行程序提示“设备未就绪”,我的问题是在这样的情况夏如何排除软驱。[VS205   WinFrom]
代码:
    DriverInfo   []drivers   =   DriverInfo.GetDrivers  
    foreach   (DriveInfo   drive   in   drivers   )
    {
        ………………………………………………
        //出错!!   设备未就绪!原因就是上面所属,
    }
大家帮忙啊!!谢谢了


------解决方案--------------------
使用 IsReady 属性测试驱动器是否已准备好,因为在未准备好的驱动器上使用此方法会引发 IOException。


下面的代码示例演示如何使用 DriveInfo 类显示有关当前系统中所有驱动器的信息。
using System;
using System.IO;

class Test
{
public static void Main()
{
DriveInfo[] allDrives = DriveInfo.GetDrives();

foreach (DriveInfo d in allDrives)
{
Console.WriteLine( "Drive {0} ", d.Name);
Console.WriteLine( " File type: {0} ", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine( " Volume label: {0} ", d.VolumeLabel);
Console.WriteLine( " File system: {0} ", d.DriveFormat);
Console.WriteLine(
" Available space to current user:{0, 15} bytes ",
d.AvailableFreeSpace);

Console.WriteLine(
" Total available space: {0, 15} bytes ",
d.TotalFreeSpace);

Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize);
}
}
}
}
/*
This code produces output similar to the following:

Drive A:\
File type: Removable
Drive C:\
File type: Fixed
Volume label:
File system: FAT32
Available space to current user: 4770430976 bytes
Total available space: 4770430976 bytes
Total size of drive: 10731683840 bytes
Drive D:\
File type: Fixed
Volume label:
File system: NTFS
Available space to current user: 15114977280 bytes
Total available space: 15114977280 bytes
Total size of drive: 25958948864 bytes
Drive E:\
File type: CDRom

The actual output of this code will vary based on machine and the permissions
granted to the user executing it.
*/
------解决方案--------------------
报错就用try,catch捕获一下即可。
------解决方案--------------------
用IsReady,