How do I write up a check that says something like this... If ANY (or if at least one) of the drive types are CDRom, then true/continue... else false (throw an error)?
Right now, I have it throwing an error for each drive check that does not pass the CDRom requirement. I think I need to use a LINQ query with Any() but I keep getting errors. I'm probably not writing it correctly.
My plan is to have a error message thrown if:
-No CD is entered
-No CD-Rom drive on computer
-CD entered is blank
-CD entered does not contain specific file needed
Here's what I have so far that doesn't work in the way I want it to:
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 && d.DriveType == DriveType.CDRom)
{
DirectoryInfo di = new DirectoryInfo(d.RootDirectory.Name);
var file = di.GetFiles("*.csv", SearchOption.AllDirectories).FirstOrDefault();
if (file == null)
{
errorwindow.Message = LanguageResources.Resource.File_Not_Found;
dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
}
else
{
foreach (FileInfo info in di.GetFiles("*.csv", SearchOption.AllDirectories))
{
Debug.Print(info.FullName);
ImportCSV(info.FullName);
break; // only looking for the first one
}
}
}
else
{
errorwindow.Message = LanguageResources.Resource.CDRom_Error;
dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
}
}
The problem currently is the loop is set up to set each drive individually first. After one drive check that is not a CD-Rom, it throws an error message and does this for each drive. I just want one unifying error message.
How do I write up a check that says something like this... If ANY (or if at least one) of the drive types are CDRom, then true/continue... else false (throw an error)?
You could try something like this:
// Get all the drives.
DriveInfo[] allDrives = DriveInfo.GetDrives();
// Check if any cdRom exists in the drives.
var cdRomExists = allDrives.Any(drive=>drive.DriveType==DriveType.CDRom);
// If at least one cd rom exists.
if(cdRomExists)
{
// Get all the cd roms.
var cdRoms = allDrives.Where(drive=>drive.DriveType==DriveType.CDRom);
// Loop through the cd roms collection.
foreach(var cdRom in cdRoms)
{
// Check if a cd is in the cdRom.
if(cdRom.IsReady)
{
}
else // the cdRom is empty.
{
}
}
}
else // There isn't any cd rom.
{
}