So, I am trying to get a query working that finds a certain flash drive attached to my computer and selects it's DriveInfo object. I am having trouble because on my system when I call the VolumeLabel property of one of my DriveInfo objects it throws an exception (IOException) because the drive is not ready to write. For some reason I have a H:\ drive which is erroring out on me, does not appear in Windows explorer, or it could be due to some other software installed on my system. Regaurdless I need to skip over any drive that throws an exception like this. Here is the code:

                var exfil = from drive in DriveInfo.GetDrives()
                        where drive.DriveType == DriveType.Removable
                            where drive.VolumeLabel == "EXFIL"
                        select drive;

I also tried looking at the Expression.TryCatch method, but I don't know how to get it working. After I get the IEnumerable object I will simply select the first drive I find, which I am sure will be the right one since nobody will name their drive what I name mine.

http://msdn.microsoft.com/en-us/library/system.linq.expressions.tryexpression.aspx

Why there are two Where clauses? Put an And between them in leu of second Where.

Am new to Linq, is kinda my first line of it. You shure it needs it? ok.

var exfil = from drive in DriveInfo.GetDrives()
where drive.DriveType == DriveType.Removable
and drive.VolumeLabel == "EXFIL"
select drive;

In any query statement like SQL or LINQ you cann't use the keyword Where more than onetime. If you have multiple checking point add them by an And keyword.

Am still getting the IOException however, "The device is not ready."

            var exfil = from drive in DriveInfo.GetDrives()
                    where drive.DriveType == DriveType.Removable 
                    && drive.VolumeLabel == "EXFIL"
                    select drive;

Ohhh! sorry. Your exception is for and drive.VolumeLabel == "EXFIL".
How could you get Volumn Lebel information if it does not contain a CD/DVD.
Remove it . Hope it could work.

Right, but there are no CDs in the drive, I think it could be some other software on the system emulating something in the background, so I need to be able to check for exceptions in the Linq code. Uninstalling the software is not really an option. We need to skip the drives that throw exceptions.

Did you try your codes after removing the line and drive.VolumeLabel == "EXFIL"? Is it works or not?

If you don't check the VolumeLabel, then yes it does work, but that means it only does half the job.

You can use IsReady function to select the drives which are ready to use. like

 var exfil = From drive In DriveInfo.GetDrives() Where drive.IsReady Select drive

What does exfil contain when you do this:

var exfil = from drive in DriveInfo.GetDrives()
                    where drive.DriveType == DriveType.Removable 
                    select drive.VolumeLabel;

Now I know that you all said that it was bad form to use multiple where statements in a query, but my compiler says it is ok, and it seems to me that I need a query from a query from a query in order to preserve some form of sequence so that this exception is not thrown. The IsReady property looks like it is the most useful, and seems to work. Here is the code I have so far that works. Do I need to combine with an && or will that just cause the IOException to come up again?

                var exfil = from drive in DriveInfo.GetDrives() 
                            where drive.IsReady 
                            where drive.DriveType == DriveType.Removable
                            where drive.VolumeLabel == "EXFIL"
                            select drive;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.