I am trying to list all local drives in my application, and DriveInfo.GetDrives
give back the local drive letters, and I need the mapped drives too.
Currently I have: C:, D:, and G: (HDD), E: and F: (CD), and S: and Z: (mapped network drives). (Yes, they are all visible in Windows Explorer, and in Total Commander too.) But only C, D, E, F, G are retrieved programmatically.
I also tried Environment.GetLogicalDrives()
, GetLogicalDriveStrings (pInvoke)
, FindFirstVolume
and FindNextVolumen
(pInvoke). I tried to find the mapped drive list in the registry. Nothing works, only the HDD and CD letters are retreived.
There isn't any exception coming, no error indicated in the direct WinAPI calls, and now I am stuck. Is it some kind of security setting? Everywhere I look, the people say that the DriveInfo.GetDrives
should give back the mapped drives. Is it really true?
I am using Vista Home Pro. The application is running from the local machine, and it is built here too with Visual Studio 2008.
I post what I used, but it is so simple, it cannot be the case that I do something wrong:
foreach (System.IO.DriveInfo di in System.IO.DriveInfo.GetDrives())
Console.WriteLine(di.Name);
Result: C:\ D:\ E:\ F:\ G:\ Press any key to continue . . .
How can I make it work?
OK, I found out how to get the disconnected drives on Vista. Not easy, but it is doable:
First, you'll need the pInvoke definition of the following WinAPI functions:
and those need a struct and some enums as well.
Then you need to call those multiple times, and at the end, you get the list. Here is the code, beware, it is long:
You've got to call the WNetResource(), and you will get back the list of drives. (and cake :-))
Environment.GetLogicalDrives()
andDriveInfo.GetDrives()
both returned all my networked drives.Is your application running as a different user (For example an asp.net website)? If it is, are the drives actually mapped for that user? You might find that the drives are mapped for you but they aren't actually mapped for the user your application is running as.