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);
}

次の問題は、コンピュータに DVD とブルーレイ ドライブの両方が搭載されているため、ドライブに CD-ROM がないことを示すエラー メッセージが 2 回続けて表示されることです。CSV ファイルを含む CD Rom がある場合、正常にインポートされますが、ブルー レイ ドライブに実行されてポップアップする foreach ループのため、別のメッセージがポップアップします。

次の状況ごとに 1 つのエラー メッセージを表示したいだけです。

私の論理は複雑すぎると思うので、論理ステートメントを調整するのに助けが必要です。

4

3 に答える 3

0

ドライブの少なくとも1 つが機能したかどうかを追跡する必要があるだけです。それらのどれもなかった場合は、エラー メッセージを出力する必要があります。他にもできることがあります ( 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 に答える