到目前为止,我有以下内容:
// 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循环会运行到蓝光驱动器并弹出。
我只想为每种情况显示一条错误消息: - 如果没有准备就绪的CD Rom并且驱动器中包含csv - 如果CD Rom驱动器不包含csv
我认为我的逻辑过于复杂,我需要帮助调整逻辑语句。
答案 0 :(得分:0)
您只需要跟踪至少一个驱动器是否适合您。如果它们都没有,那么您想输出错误消息。还有其他一些你可以做的事情(不需要Any
/ Where
,也不需要做.Equals(true)
等等。更具体地说,不需要不断检查它是否是正确类型的驱动器。cdRoms
集合将只包含具有正确类型的驱动器,因为这是您在Where
子句中指定的内容。
// Gets all the drives
DriveInfo[] allDrives = DriveInfo.GetDrives();
// Get all the cd roms
var cdRoms = allDrives.Where(x=>x.DriveType==DriveType.CDRom);
if (cdRoms.Count() > 0)
{
bool found = false;
// 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)
{
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);
found = true;
break; // only looking for the first one
}
}
}
else
{
Debug.Print(string.Format("Drive {0} is not ready", cdRom.Name));
}
}
if (!found)
{
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);
}
答案 1 :(得分:0)
您的代码可以重写为以下内容:
var cdRoms = allDrives.Where(x => x.DriveType == DriveType.CDRom && x.IsReady);
if (cdRoms.Any())
{
foreach(var cdRom in cdRoms)
{
Console.WriteLine("Drive {0}", cdRom.Name);
Console.WriteLine(" File type: {0}", cdRom.DriveType);
var 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 (var info in di.GetFiles("*.csv", SearchOption.AllDirectories))
{
Debug.Print(info.FullName);
ImportCSV(info.FullName);
break;
}
}
}
}
else
{
errorwindow.Message = LanguageResources.Resource.CDRom_Error;
dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
}
的变化:
Where
和Any
,从所有驱动器中过滤CD-Rom驱动器并查看是否存在var
,让编译器完成工作答案 2 :(得分:0)
这是我解决问题的方法:
bool anyCdrom = false;
bool anyReady = false;
bool fileFound = false;
// Loop through the cd roms collection
foreach(var cdRom in DriveInfo.GetDrives().Where(drive => drive.DriveType == DriveType.CDRom))
{
anyCdrom = true;
Console.WriteLine("Drive {0}", cdRom.Name);
Console.WriteLine(" File type: {0}", cdRom.DriveType);
if (cdRom.IsReady) // You may want to put in into the intial where
{
anyReady = true;
foreach (string file in Directory.EnumerateFiles(cdRom.RootDirectory.Name, "*.csv", SearchOption.AllDirectories))
{
fileFound = true;
Debug.Print(file);
ImportCSV(file);
break; // only looking for the first one
}
if(fileFound)
break;
}
}
if(!anyCdrom)
{
errorwindow.Message = LanguageResources.Resource.CDRom_Error;
dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
}
else if(!anyReady)
{
errorwindow.Message = LanguageResources.Resource.CDRom_Not_Ready;
dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
}
else if(!fileFound)
{
errorwindow.Message = LanguageResources.Resource.File_Not_Found;
dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
}
仅在以下情况下打印错误: