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 的 CD ROM - 如果 CD ROM 驱动器不包含 csv

我认为我的逻辑太复杂了,我需要帮助调整我的逻辑语句。

4

3 回答 3

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. 没有准备好 cdrom
  3. 任何准备好的 cdrom 中都没有 csv 文件
于 2014-07-23T12:32:04.033 回答
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);
}
于 2014-07-23T12:25:18.010 回答
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,让编译器完成工作
于 2014-07-23T12:28:08.487 回答