How can I know that a driver is unavailable?

When I launch the sentence below
string[] a1=Directory.GetDirectories("A:\")
I get a error because I don't put a floppy disk in driver A:

How can I know that a driver is unavailable Do I need to write the sentences below Is there a better way

try {
string[] a1=Directory.GetDirectories("A:\")
}
catch {
MessageBox.Show("A driver is unavailable!");
}



Answer this question

How can I know that a driver is unavailable?

  • vvgalc

    Who know thanks!

  • Leandro Mussi

    Hi CUI WEI,

    You are correct in your approach. In case of drives which can have removable media such as floppy drives and cd/dvd rom drives, you need to manage by catching the Exception.

    Regards,
    Vikram

  • Rahul_It

    Hello CUE WEI,

    I recommend catching the most limited set of exceptions possible (e.g. only DirectoryNotFoundException). There are a number of exceptions that can be thrown in code, and narrowing your error paths and recovery logic will be better for your code maintenance.

    On a related note, you may want to look at Directory.Exists instead of GetDirectories to avoid the exception, depending on your scenario. Of course, calling Directory.Exists, then using the Directory doesn't prevent the user from pulling out the cd/floppy between the two calls, so you'll need to be tolerant to that possibility in your code (e.g. exception handling).

    Hope that helps,
    Stephen
    http://blogs.msdn.com/stfisher

  • How can I know that a driver is unavailable?