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

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

    Inspired by map network drive path in C# here's another simple method using Scripting objects:

                private static IDictionary GetMappedNetworkDrives()
            {
                var rawDrives = new IWshRuntimeLibrary.IWshNetwork_Class()
                    .EnumNetworkDrives();
                var result = new Dictionary(
                    rawDrives.length / 2);
                for (int i = 0; i < rawDrives.length; i += 2)
                {
                    result.Add(
                        new DriveInfo(rawDrives.Item(i)),
                        rawDrives.Item(i + 1));
                }
                return result;
            }
    

    See https://msdn.microsoft.com/en-us/library/t9zt39at(v=vs.84).aspx for details about the IWshNetwork_Class.

提交回复
热议问题