So far I have the following:
// Gets all the drives
DriveInfo[] allDrives = DriveInfo.GetDrives();
// checks if any CD-Rom exists in the drives
var cdRomExists = allDrives.Any(x => x.DriveType == DriveType.CDRom);
// Get all the cd roms
var cdRoms = allDrives.Where(x=>x.DriveType==DriveType.CDRom);
if (cdRomExists.Equals(true))
{
// Loop through the cd roms collection
foreach(var cdRom in cdRoms)
{
Console.WriteLine("Drive {0}", cdRom.Name);
Console.WriteLine(" File type: {0}", cdRom.DriveType);
if (cdRom.IsReady == true)
{
if (cdRom.DriveType == DriveType.CDRom)
{
DirectoryInfo di = new DirectoryInfo(cdRom.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 if (cdRom.IsReady == false)
{
errorwindow.Message = LanguageResources.Resource.CDRom_Not_Ready;
dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
}
}
}
else
{
errorwindow.Message = LanguageResources.Resource.CDRom_Error;
dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
}
The problem with the following is, an error message pops up twice in a row to indicate if there is no CD-ROM in the drive because my computer contains both a DVD and blu ray Drive. If there is a CD Rom that contains CSV file, it gets successfully imported but another message pops up because of the foreach loop that runs to the blu ray drive and pops up.
I only want to display one error message for each of these situations: -If there is no CD Rom that is ready and contains csv in the drive -If the CD Rom drive does not contain csv
I think my logic is too convoluted and I need help adjusting my logic statements.