1

How do I write up a check that says something like this... If ANY (or if at least one) of the drive types are CDRom, then true/continue... else false (throw an error)?

Right now, I have it throwing an error for each drive check that does not pass the CDRom requirement. I think I need to use a LINQ query with Any() but I keep getting errors. I'm probably not writing it correctly.

My plan is to have a error message thrown if:

-No CD is entered

-No CD-Rom drive on computer

-CD entered is blank

-CD entered does not contain specific file needed

Here's what I have so far that doesn't work in the way I want it to:

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

The problem currently is the loop is set up to set each drive individually first. After one drive check that is not a CD-Rom, it throws an error message and does this for each drive. I just want one unifying error message.

Kala J
  • 2,040
  • 4
  • 45
  • 85

2 Answers2

5

How do I write up a check that says something like this... If ANY (or if at least one) of the drive types are CDRom, then true/continue... else false (throw an error)?

You could try something like this:

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

}
Christos
  • 53,228
  • 8
  • 76
  • 108
  • That does work but I am noticing it still leaves me with some problems. If it detects a CD-Rom but there's no CD inside, the conditional is true and does not throw a message. I could fix this by checking if d.IsReady = true or not. However, it would still loop for every drive if I use it after foreach (DriveInfo d in allDrives) {} – Kala J Jul 10 '14 at 15:13
  • please see my updated post and let me know if that's suits in your case. Thanks ! – Christos Jul 10 '14 at 15:13
  • Thanks Christos. It looks like it will. Let me test it really quickly. Thanks again. – Kala J Jul 10 '14 at 15:14
  • You welcome. Sure thing dude, take your time. Just let me know about the results. Thanks – Christos Jul 10 '14 at 15:15
  • Change IsReady() to IsReady (Not a method). It looks to be working. I have another question, e.g. I have two CDRom drives on my computer, it throws a CD Rom is empty message twice. In my case, I just want to detect the CDRom drive that contains a CSV file (There will be only one CSV file per CD). I should just add another boolean that checks if CDRom contains file or not? – Kala J Jul 10 '14 at 15:26
  • @KalaJ Oops you are right the `IsReady`is a property and not a method. I apologize :). – Christos Jul 10 '14 at 15:28
  • @KalaJ as for you second question, you could do this checking inside the ` if(cdRom.IsReady) { }`, because you know that this cdRom contains a cd and you can read from this to look if there exists this file. – Christos Jul 10 '14 at 15:30
  • 1
    @AmrSubZero you are welcome dude ! I am glad that I helped :) – Christos Aug 10 '15 at 08:38
1

You're correct that LINQ can help, in fact it can shorten up the code a lot:

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;                        
}
Alex
  • 23,004
  • 4
  • 39
  • 73