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

前端 未结 7 1365
星月不相逢
星月不相逢 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

    You could use WMI to enumerate and query mapped drives. The following code enumerates mapped drives, extracts the server name portion, and prints that out.

    using System;
    using System.Text.RegularExpressions;
    using System.Management;
    
    namespace ConsoleApplication1 {
        class Program {
            static void Main(string[] args) {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(
                    "select * from Win32_MappedLogicalDisk");
                foreach (ManagementObject drive in searcher.Get()) {
                    Console.WriteLine(Regex.Match(
                        drive["ProviderName"].ToString(),
                        @"\\\\([^\\]+)").Groups[1]);
                    }
                }
            }
        }
    }
    

    You can find the documentaiton of the Win32_MappedLogicalDisk class here. An intro for accessing WMI from C# is here.

提交回复
热议问题