首页 > 解决方案 > 使用 WMI 列出映射的网络驱动器

问题描述

我有一个用 c# 编写的应用程序,它创建了所有映射的网络驱动器的列表。

我使用 WMI 来实现这一点,我是这样做的:

List < NetworkConnection > networkConnections = new List < NetworkConnection > ();

try {
 var path = new ManagementPath();
 path.NamespacePath = "\\ROOT\\CIMV2";
 path.ClassName = "Win32_NetworkConnection"; //https://msdn.microsoft.com/en-us/library/aa394220(v=vs.85).aspx

 var management = new ManagementClass(path);

 foreach(ManagementObject obj in management.GetInstances()) {
  NetworkConnection networkConnection = new NetworkConnection() {
   LocalName = (string) obj.Properties["LocalName"].Value,
    Name = (string) obj.Properties["Name"].Value,
    RemotePath = (string) obj.Properties["RemotePath"].Value,
    UserName = (string) obj.Properties["UserName"].Value,
    IsPersistent = (bool) obj.Properties["Persistent"].Value,
    ConnectionState = (string) obj.Properties["ConnectionState"].Value
  };

  networkConnections.Add(networkConnection);
 }
} catch (Exception ex) {
 log.Error($ "Failed to get network connections.", ex);
}

return networkConnections;

虽然这确实适用于我的测试客户端 W7 和 W8 64 位和 32 位。它不适用于我的 W10 客户端。在 W10 上,我只得到一个空列表。

看起来 management.GetInstances() 没有返回任何内容。但为什么?

任何帮助是极大的赞赏!谢谢!

标签: c#wminetwork-drive

解决方案


推荐阅读