我有这段代码:
DriveInfo[] myDrives = DriveInfo.GetDrives();
foreach (DriveInfo myDrive in myDrives)
{
if (myDrive.DriveType == DriveType.Removable)
{
string path = Convert.ToString(myDrive.RootDirectory);
DirectoryInfo mydir = new DirectoryInfo(path);
String[] dirs = new string[] {Convert.ToString(mydir.GetDirectories())};
String[] files = new string[] {Convert.ToString(mydir.GetFiles())};
foreach (var file in files)
{
File.SetAttributes(file, ~FileAttributes.Hidden);
File.SetAttributes(file, ~FileAttributes.ReadOnly);
}
foreach (var dir in dirs)
{
File.SetAttributes(dir, ~FileAttributes.Hidden);
File.SetAttributes(dir, ~FileAttributes.ReadOnly);
}
}
}
我有问题。它首先尝试软盘驱动器的代码,因为它没有软盘,它抛出了错误The device is not ready.
我该如何防止这种情况?