簡體   English   中英

布爾檢查是否有任何驅動器包含特定的驅動器類型

[英]Boolean check if any of the drives contain a specific drivetype

我該如何寫類似這樣的檢查...如果任何(或至少一種)驅動器類型為CDRom,則為true / continue ...否則為false(引發錯誤)?

現在,我為每個未通過CDRom要求的驅動器檢查拋出一個錯誤。 我想我需要對ANY()使用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的一個驅動器后,它會引發錯誤消息,並對每個驅動器執行此操作。 我只想要一個統一的錯誤消息。

我該如何寫類似這樣的檢查...如果任何(或至少一種)驅動器類型為CDRom,則為true / continue ...否則為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.
{

}

您是正確的,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;                        
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM