简体   繁体   English

从C#中的远程计算机获取映射的网络驱动器

[英]Get Mapped Network drives from remote machine in C#

I want to list mapped network drives in remote machine using WMI C#.. I am using following code 我想使用WMI C#列出远程计算机中的映射网络驱动器。

      ConnectionOptions connectionOptions = new ConnectionOptions();
            connectionOptions.Username = "myAdminUser";
            connectionOptions.Password = "Password";
            connectionOptions.Impersonation = ImpersonationLevel.Impersonate;

            ManagementScope Scope = new ManagementScope(@"\\" + "myClientMachine" + @"\root\cimv2", connectionOptions);
            Scope.Connect();
            ManagementObjectSearcher win32Drives = new ManagementObjectSearcher(Scope,
 new ObjectQuery(@"SELECT Name,UserName FROM Win32_NetworkConnection'"));
            foreach (ManagementObject DriveData in win32Drives.Get())
            {
                string drivePath = (string)DriveData["Name"];
                string userName = (string)DriveData["UserName"];
            } 

I am running this code in my Server Machine with Admin Credentials to get mapped drives from 我在具有管理凭据的服务器计算机上运行此代码,以从中获取映射的驱动器
my Client Machine... this code returns 0 results when I use Admin credentials .. but at the same time when I use my client user credentials it returns mapped drives for the client user. 我的客户端计算机...当我使用管理员凭据..时,此代码返回0个结果,但同时,当我使用客户端用户凭据时,它为客户端用户返回映射的驱动器。

Here, my question is, is there any way to get all the mapped drives in client Machine for the all the users? 在这里,我的问题是,有没有办法为所有用户在客户端计算机中获取所有映射的驱动器?

Yes you can utilize the winmgmts queries and retrieve them from a scripted scan of the computer via vbscript 是的,您可以利用winmgmts查询并通过vbscript从计算机的脚本扫描中检索它们

'Define variables, constants and objects 

strComputer="<remote machine here>" 
Const HKEY_USERS = &H80000003 
Set objWbem = GetObject("winmgmts:") 
Set objRegistry = GetObject("winmgmts://" & strComputer & "/root/default:StdRegProv") 
Set objWMIService = GetObject("winmgmts:"  & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

'Go and get the currently logged on user by checking the owner of the Explorer.exe process.   

Set colProc = objWmiService.ExecQuery("Select Name from Win32_Process" & " Where Name='explorer.exe' and SessionID=0") 

If colProc.Count > 0 Then 
    For Each oProcess In colProc 
        oProcess.GetOwner sUser, sDomain 
    Next 
End If 

'Loop through the HKEY_USERS hive until (ignoring the .DEFAULT and _CLASSES trees) until we find the tree that  
'corresponds to the currently logged on user. 
lngRtn = objRegistry.EnumKey(HKEY_USERS, "", arrRegKeys)     

For Each strKey In arrRegKeys 
    If UCase(strKey) = ".DEFAULT" Or UCase(Right(strKey, 8)) = "_CLASSES" Then 
    Else 

        Set objSID = objWbem.Get("Win32_SID.SID='" & strKey & "'") 

'If the account name of the current sid we're checking matches the accountname we're looking for Then 
'enumerate the Network subtree 
        If objSID.accountname = sUser Then  
            regpath2enumerate = strkey & "\Network" 'strkey is the SID 
            objRegistry.enumkey hkey_users, regpath2enumerate, arrkeynames 

'If the array has elements, go and get the drives info from the registry 
            If Not (IsEmpty(arrkeynames)) Then 
                For Each subkey In arrkeynames 
                    regpath = strkey & "\Network\" & subkey 
                    regentry = "RemotePath" 
                    objRegistry.getstringvalue hkey_users, regpath, regentry, dapath 
                    wscript.echo subkey & ":" & vbTab & dapath 
                Next 
            End If 
        End If 
    End If 
Next 

http://gallery.technet.microsoft.com/scriptcenter/3dd6af3e-edfa-4581-bc35-805314f26bb8 http://gallery.technet.microsoft.com/scriptcenter/3dd6af3e-edfa-4581-bc35-805314f26bb8

Or you can utilize the C# version: 或者您可以使用C#版本:

http://bytes.com/topic/net/answers/170583-list-mapped-drives-remote-machine http://bytes.com/topic/net/answers/170583-list-mapped-drives-remote-machine

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM