我有一个必须以管理员身份运行的c#应用程序。除了很多其他事情,应用程序还必须读取映射的网络驱动器。
我想用WMI和Win32_NetworkConnection做到这一点,但是这只能作为非管理员使用。
是否有解决方法?我最好的选择是什么?
提前感谢!
编辑:
这里是即使连接了3个网络驱动器,我仍然返回一个空列表的代码:
public IEnumerable<NetworkConnection> GetNetworkConnections()
{
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;
}
我尝试了一些操作,但这是我发现的唯一解决方法。
如何启用EnableLinkedConnections
单击开始,在开始程序和文件框中键入regedit,然后然后按Enter。
找到并右键单击注册表子项HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Policies \ System。
指向新建,然后单击DWORD值。
Type EnableLinkedConnections,然后按ENTER。
右键单击EnableLinkedConnections,然后单击修改。
在“数值数据”框中,键入1,然后单击确定。
退出注册表编辑器,然后重新启动计算机。