【问题标题】:Boolean check if any of the drives contain a specific drivetype布尔检查是否有任何驱动器包含特定的驱动器类型
【发布时间】:2014-09-01 00:36:10
【问题描述】:

我如何写出这样的支票...如果任何(或至少一个)驱动器类型是 CDRom,则为真/继续...否则为假(抛出错误)?

现在,我让它为每个未通过 CDRom 要求的驱动器检查抛出一个错误。我想我需要使用带有 Any() 的 LINQ 查询,但我不断收到错误。我可能写得不对。

我的计划是在以下情况下抛出错误消息:

-没有CD进入

-电脑上没有光驱

-输入的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 的驱动器后,它会抛出一条错误消息并为每个驱动器执行此操作。我只想要一个统一的错误消息。

【问题讨论】:

    标签: c# boolean any driveinfo


    【解决方案1】:

    我如何写出这样的支票...如果有(或 如果至少一种)驱动器类型是 CDRom,则为真/继续... else 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.
    {
    
    }
    

    【讨论】:

    • 这确实有效,但我注意到它仍然给我留下了一些问题。如果它检测到 CD-Rom 但里面没有 CD,则条件为真并且不抛出消息。我可以通过检查 d.IsReady = true 与否来解决这个问题。但是,如果我在 foreach (DriveInfo d in allDrives) {} 之后使用它,它仍然会为每个驱动器循环
    • 请查看我更新的帖子,让我知道这是否适合您的情况。谢谢!
    • 谢谢克里斯托斯。看起来会的。让我快速测试一下。再次感谢。
    • 欢迎您。没问题的家伙,慢慢来。让我知道结果。谢谢
    • @AmrSubZero 不客气,伙计!我很高兴能帮上忙:)
    【解决方案2】:

    你说得对,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;                        
    }
    

    【讨论】:

    • 感谢您的回复。很高兴看到更简洁的替代方案。
    猜你喜欢
    • 2010-09-14
    • 2010-09-14
    • 2012-06-22
    • 2015-10-08
    • 1970-01-01
    • 2011-05-22
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多