1

我如何写出这样的支票...如果任何(或至少一个)驱动器类型是 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 的驱动器后,它会抛出一条错误消息并为每个驱动器执行此操作。我只想要一个统一的错误消息。

4

2 回答 2

5

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

你可以尝试这样的事情:

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

}
于 2014-07-10T15:03:07.660 回答
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;                        
}
于 2014-07-10T15:31:58.567 回答