Search code examples
c#windowsnetwork-programmingwindows-10

How do I check programmatically whether a network drive has been mapped?


Currently I'm using System.IO.Directory.Exists("Z:\\") to check. However, it still returns true even after I have disconnected the network drive using File Explorer. It also still returns true after I have mapped it programmatically eventhough the Z Drive isn't showing in File Explorer.

How do I check whether the network drive is mapped by using c# or by using the command line?

My OS is Windows 10


Solution

  • I just powered off a mapped network drive in windows 7 and got false for Directory.Exists(). The linked question has about 10 different answers. This seems complex.

    I would take a different approach.

    try Directory.EnumerateDirectories("Z:\\") or some other mechanism for actually listing the contents of the drive and see if it throws an exception or fails to return anything (hopefully you could count on something being there). Even if there's 10 different ways a drive can be connected or not connected, for most of our purposes, if you can't access its contents, it's not connected.