2014-07-23 41 views
0

到目前为止,我有以下几点:简化的逻辑,以避免重复错误信息

// 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不包含CSV

我认为我的逻辑过于复杂,我需要帮助调整我的逻辑陈述。

+1

使用'break'如果没有csv文件发生一些错误(在MessageBox之后)。 –

+0

如果将原始查询调整为仅将已准备就绪的驱动器撤回,则可能会更容易。你也不需要'Any()'和'Where()'调用;如果没有这样的驱动器,那么'Where()'将返回一个长度为0的序列,您可以事先对其进行测试,或者只是对序列进行迭代,因为对空序列不做任何处理。 – barrick

回答

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); 
} 
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); 
} 

变化:

  • 没有必要使用WhereAny像你这样,从所有驱动器过滤CD-ROM驱动器,看看是否存在任何
  • 只能选择驱动器是 CD-ROM驱动器准备
  • 使用var如果可能的话,让编译器做的工作
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); 
} 

它只打印时出现错误:

  1. 没有光驱
  2. 没有光驱准备
  3. 存在任何现成光盘