【问题标题】:Get Mapped Network drives from remote machine in C#从 C# 中的远程计算机获取映射的网络驱动器
【发布时间】:2014-03-24 06:14:41
【问题描述】:

我想使用 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"];
            } 

我正在使用管理员凭据在我的服务器计算机上运行此代码以从
获取映射驱动器 我的客户端机器...当我使用管理员凭据时,此代码返回 0 个结果..但同时当我使用我的客户端用户凭据时,它返回客户端用户的映射驱动器。

在这里,我的问题是,有没有办法为所有用户获取客户端机器中的所有映射驱动器?

【问题讨论】:

    标签: c# vbscript network-programming wmi-query


    【解决方案1】:

    是的,您可以利用 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

    或者您可以使用 C# 版本:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-04
      • 1970-01-01
      • 2021-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多