简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM