Thursday, October 15, 2015

Get Mapped drives remotely in .NET with WMI

     public List<string> GetMappedDrives(string pcname)  
     {  
       pcname = CleanPCName(pcname);  
       List<string> resultList = WMIGetMappedDrives(pcname);  
       List<string> resultList2 = RegGetMappedDrives(pcname);  
       resultList.Add("");  
       List<string> combinedList = new List<string>();  
       combinedList.AddRange(resultList);  
       combinedList.AddRange(resultList2);  
       return combinedList;  
     }  
     private List<string> WMIGetMappedDrives(string pcname)  
     {  
       List<string> resultList = new List<string>();  
       string query = "Select * from Win32_MappedLogicalDisk";  
       StringBuilder queryResult = new StringBuilder();  
       try  
       {  
         ManagementScope scope = new ManagementScope(backslashes + pcname + @"\root\cimv2");  
         scope.Connect();  
         ObjectQuery obquery = new ObjectQuery(query);  
         ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, obquery);  
         foreach (ManagementObject obj in searcher.Get())  
         {  
           try  
           {  
             resultList.Add(obj["Name"].ToString() + " " + obj["ProviderName"]);  
           }  
           catch  
           {  
             resultList.Add("Unknown");  
           }  
         }  
       }  
       catch (Exception e)  
       {  
         MessageBox.Show(e.Message);  
       }  
       //resultList.Sort();  
       return resultList;  
     }  
     private List<string> RegGetMappedDrives(string pcname)  
     {  
       string sid = WmiGetUserSID(pcname, WmiGetExplorerProcessOwner(pcname));  
       pcname = CleanPCName(pcname);  
       RegistryKey regKey;  
       StringBuilder queryResult = new StringBuilder();  
       List<string> resultList = new List<string>();  
       try  
       {  
         regKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.Users, pcname).OpenSubKey(sid + @"\\Network");  
         //PrintKeys(rk);  
         foreach (string subKeyName in regKey.GetSubKeyNames())  
         {  
           try  
           {  
             string strPath = regKey.OpenSubKey(subKeyName).GetValue("RemotePath").ToString();  
             resultList.Add(subKeyName + ": " + strPath);  
           }  
           catch  
           {  
             resultList.Add("Unknown");  
           }  
         }  
         regKey.Close();  
         resultList.Sort();  
         regKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.Users, pcname).OpenSubKey(sid + @"\\Volatile Environment");  
         resultList.Add(regKey.GetValue("HOMEDRIVE").ToString() + " " + regKey.GetValue("HOMESHARE").ToString() + " (HOMEDRIVE setting)");  
         regKey.Close();  
       }  
       catch //(Exception e)  
       {  
         //MessageBox.Show(e.GetType().Name + ": " + e.Message);  
         resultList.Add("");  
       }  
       return resultList;  
     }  
     private string WmiGetUserSID(string pcname, string domainBslashAccount)  
     {  
       string domain, user;  
       domain = domainBslashAccount.Substring(0, domainBslashAccount.IndexOf(@"\"));  
       user = domainBslashAccount.Substring(domainBslashAccount.IndexOf(@"\") + 1);  
       //string query = "Select * from Win32_UserAccount" + " Where Name='hadye' and Domain='domain'";  
       string query = "Select * from Win32_UserAccount" + " Where Name='" + user + "' and Domain='" + domain + "'";  
       ManagementScope scope = new ManagementScope(backslashes + pcname + @"\root\cimv2");  
       string sid = "unknown";  
       StringBuilder queryResult = new StringBuilder();  
       List<string> resultList = new List<string>();  
       pcname = CleanPCName(pcname);  
       try  
       {  
         scope.Connect();  
         ObjectQuery obquery = new ObjectQuery(query);  
         ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, obquery);  
         foreach (ManagementObject obj in searcher.Get())  
         {  
           try  
           {  
             sid = obj["SID"].ToString();  
             return sid;  
           }  
           catch (Exception e)  
           {  
             MessageBox.Show(e.Message);  
             return sid;  
           }  
         }  
         return sid;  
       }  
       catch (Exception e)  
       {  
         MessageBox.Show(e.Message);  
         return "SID unknown";  
       }  
     }  
     private string WmiGetExplorerProcessOwner(string pcname)  
     {  
       string query = "Select * from Win32_Process" + " Where Name='explorer.exe'"; // and SessionID=0"; sessionid=0 is not correct on widnows7  
       ManagementScope scope = new ManagementScope(backslashes + pcname + @"\root\cimv2");  
       StringBuilder queryResult = new StringBuilder();  
       List<string> resultList = new List<string>();  
       pcname = CleanPCName(pcname);  
       try  
       {  
         scope.Connect();  
         ObjectQuery obquery = new ObjectQuery(query);  
         ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, obquery);  
         //MessageBox.Show(searcher.Get().Count.ToString());  
         if (searcher.Get().Count >= 0)  
         {  
           ManagementObjectCollection processList = searcher.Get();  
           foreach (ManagementObject obj in processList)  
           {  
             string[] argList = new string[] { string.Empty, string.Empty };  
             int returnVal = Convert.ToInt32(obj.InvokeMethod("GetOwner", argList));  
             if (returnVal == 0)  
             {  
               // return DOMAIN\user  
               string owner = argList[1] + "\\" + argList[0];  
               return owner;  
             }  
           }  
         }  
         else  
         {  
           return @"localhost\administrator";  
         }  
         return @"localhost\administrator";  
       }  
       catch (Exception e)  
       {  
         MessageBox.Show(e.Message);  
         return @"localhost\administrator";  
       }  
     }  

No comments:

Post a Comment