How to programmatically discover mapped network drives on system and their server names?

前端 未结 7 1367
星月不相逢
星月不相逢 2020-11-28 06:28

I\'m trying to find out how to programmatically (I\'m using C#) determine the name (or i.p.) of servers to which my workstation has current maps. In other words, at some po

7条回答
  •  再見小時候
    2020-11-28 07:16

    The WMI methods won't tell you whether the drive is set to reconnect on login. When you set a drive to reconnect on login, Windows creates a key under HKCU\Network\. The method below can be used to determine if the drive is set to be remapped at login.

    private static bool DriveSetForReconnect(string ComputerName, string DriveLetter)
    {
        RegistryKey key = RegistryKey.OpenRemoteBaseKey(RegistryHive.CurrentUser, ComputerName);
        key = key.OpenSubKey("Network\\" + DriveLetter);
    
        return key != null;
    }
    

    HTH!

    EDIT: To adapt the WMI solutions to work on any arbitrary machine, you need to change the scope parameter like the code below. You obviously have to have have admin rights on the remote machine.

    string scope = string.Format(@"\\{0}\root\CIMV2", ComputerName);
    
    ManagementObjectSearcher searcher =
        new ManagementObjectSearcher(scope,
        "SELECT * FROM Win32_MappedLogicalDisk");
    

提交回复
热议问题