简化逻辑以避免重复的错误消息

本文关键字:错误 消息 | 更新日期: 2023-09-27 18:14:35

目前我有以下内容:

// 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循环运行到蓝光驱动器并弹出。

我只想在每种情况下显示一个错误消息:-如果没有准备好的CD Rom,并且在驱动器中包含csv-如果光驱中不包含csv

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

简化逻辑以避免重复的错误消息

您只需要跟踪是否至少有一个驱动器为您工作。如果它们都没有,那么您希望输出错误消息。你还可以做一些其他的事情(不需要做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);
}

您的代码可以重写为:

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,让编译器完成这项工作

这是我解决你问题的方法:

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. 没有cdrom
  2. 没有现成的光盘
  3. 文件中没有csv文件