2014-07-10 36 views
1

如何寫出類似這樣的檢查......如果ANY(或至少一個)驅動器類型是CDRom,則爲true/continue。 ..其他錯誤(拋出錯誤)?布爾檢查是否有任何驅動器包含特定的驅動器類型

現在,我已經爲每個驅動器檢查沒有通過CDRom要求拋出一個錯誤。我想我需要使用任何()的LINQ查詢,但我不斷收到錯誤。我可能沒有正確寫入。

我的計劃是,如果已經拋出一個錯誤信息:

- 無CD輸入

- 無CD-ROM驅動器上輸入計算機

-CD是空白

- 輸入的CD不包含所需的特定文件

以下是我迄今爲止無法按照我希望的方式工作:

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

目前的問題是循環設置爲首先單獨設置每個驅動器。在一次驅動器檢查不是CD-Rom後,它會拋出一條錯誤消息併爲每個驅動器執行此操作。我只想要一個統一的錯誤消息。

回答

5

我怎麼寫了一張支票,上面寫着這樣的事情......如果驅動器類型的任何(或 如果至少一個)是CDROM,則真/繼續... 否則返回false(投一個錯誤)?

你可以嘗試這樣的事:

// 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. 
{ 

} 
+0

這的工作,但我注意到它仍然給我留下了一些問題。如果它檢測到CD-Rom但內部沒有CD,則條件爲真,並且不會顯示消息。我可以通過檢查d.IsReady = true來解決這個問題。但是,如果我在foreach後使用它(DriveInfo d在allDrives中),它仍然會爲每個驅動器循環。 {} –

+0

請查看我的更新後的帖子,並讓我知道這是否適合您的情況。謝謝 ! – Christos

+0

謝謝Christos。它看起來會。讓我真的很快測試它。再次感謝。 –

1

你是正確的,LINQ可以幫助,事實上,它可以縮短了代碼很多:

var readyCDRoms = DriveInfo.GetDrives().Any(drive => 
    drive.DriveType == DriveType.CDRom && drive.IsReady); 

foreach(var drive in readyCDRoms) 
{ 
    var file = new DirectoryInfo(drive.RootDirectory.Name) 
     .GetFiles(/* blah */).FirstOrDefault(); //only looking for the first one 

    if(file == null) continue; // No suitable files found 

    Debug.Print(file.FullName); 
    ImportCSV(file.FullName);  

    //Remove the break; to parse all CDROMS containing viable data 
    break;       
} 
+0

感謝您的回覆。很高興看到更簡潔的替代方案。 –

相關問題