1

Is it possible to obtain all drives? For example, the network drives are not connected due to lack of authentication.

I see in my explorer, e.g. the letter Z with a red cross. This connection is stored without authentication, but this code does not give me this letter.

System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();

And: how can I register a listener for connection of an USB-drive?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
raiserle
  • 677
  • 8
  • 31
  • 3
    Please ask one question per post. I've removed second question "how to listen to USB connect event" from the post (along with "thank you" notes) – Alexei Levenkov Jun 05 '16 at 18:44
  • Maybe this will be of some help for you? http://stackoverflow.com/questions/1088752/how-to-programmatically-discover-mapped-network-drives-on-system-and-their-serve – Marek M. Jun 05 '16 at 20:16
  • You mean, use of WMI? I think, this can work. But wmi-calls very slow :( Another solution would be great. – raiserle Jun 05 '16 at 21:32

1 Answers1

-1

Referencing the example and info from Microsoft https://msdn.microsoft.com/en-us/library/system.io.driveinfo.getdrives(v=vs.110).aspx, I found the below code to produce the drive letter and type of all drives including those not currently connected but allocated.

using System;
using System.IO;

namespace DriveInfoExample
{
    class Program
    {
        static void Main(string[] args)
        {
            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo d in drives)
            {
                // The only two properties that can be accessed for all drives
                // whether they are online or not (ready)
                //
                Console.WriteLine(d.Name);
                Console.WriteLine(d.DriveType);
            }
            Console.ReadLine();
        }
    }
}

Per the instructions on the link above, if you try to get other attributes on a drive the is not ready, an IOException will be thrown.

One way to handle this is to check drive.IsReady for true with an if statement before getting the other properties (as shown below and almost directly ripped from the link above):

using System;
using System.IO;

class DriveInfoExample
{
    public static void Main()
    {
        DriveInfo[] drives = DriveInfo.GetDrives();

        foreach (DriveInfo d in drives)
        {
            Console.WriteLine(d.Name);
            Console.WriteLine(d.DriveType);
            if (d.IsReady == true)
            {
                Console.WriteLine(d.VolumeLabel);
                Console.WriteLine(d.DriveFormat);
                Console.WriteLine(d.AvailableFreeSpace);
                Console.WriteLine(d.TotalFreeSpace);
                Console.WriteLine(d.TotalSize);
            }
        }
        Console.ReadLine();
    }
}

The key in the above example is the if (d.IsReady == true) as it will only get attributes for drives that are considered ready and you wont throw an IO Exception.

  • 1
    this code does not work. in my case: I've a letter (z), this was mapped to a network-path. But the authentication is not available on network-path. In the explorer, you can see the mapped drive letter by Z: with a red cross. The function ``DriveInfo.GetDrives()`` give no entry for this letter so i can check for ready state. – raiserle Jun 05 '16 at 21:30
  • _**Please post the code you are using when trying to get the drive letter Z to show up.**_ Without the code, it will be difficult to track down your issue as the above code works fine in my test case and has a `driveinfo` object in the `drives` array for every drive shown in explorer-- including those that are currently disconnected. – Zach Washburn Jun 05 '16 at 22:21
  • Another potential issue with the above would be user context. For example, the problem mentioned in the OP can be replicated with the above code in Windows 7 if the code is being run as administrator and the mapped drive was created by the logged in user. However, this problem does not present in Windows 2008. For further debugging, more information is needed about the OS and privileges used to execute the code. – Zach Washburn Jun 06 '16 at 01:12
  • I see you have my question now read. My OS is Win7, UAC is disabled, the code is running under the user who has mapped the network drive. The user is in the administrators group. – raiserle Jun 06 '16 at 08:04
  • 1
    This code does not work. Unmapped drives are NOT given from DriveInfo.GetDrives() as raiserle already said. I have the same problem. – JohnnyBravo75 Feb 09 '21 at 15:30