到目前为止,我有以下内容:
// 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);
}
下面的问题是,连续两次弹出错误信息提示驱动器中是否没有CD-ROM,因为我的电脑同时包含DVD和蓝光驱动器。如果存在包含 CSV 文件的 CD ROM,它会成功导入,但由于运行到蓝光驱动器并弹出的 foreach 循环,会弹出另一条消息。
我只想针对以下每种情况显示一条错误消息: - 如果没有准备好并且驱动器中包含 csv 的 CD ROM - 如果 CD ROM 驱动器不包含 csv
我认为我的逻辑太复杂了,我需要帮助调整我的逻辑语句。