2014-07-04 68 views
2

如何獲取安裝在網絡計算機上的軟件列表。按IP地址列出網絡計算機上安裝的所有軟件

我能夠獲得爲本地計算機安裝的軟件列表,但不知道如何在網絡中的計算機上提取已安裝軟件的詳細信息。

我有網絡計算機的服務器名稱或IP地址作爲唯一的輸入。請引導我並幫助我實現這一目標。

下面是我執行了,直到如今它正從本地機器上安裝軟件的詳細信息代碼:

const string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; 

private void ListSoftwareInstalled(string servername) 
{ 
    var result = new List<string>(); 
    result.AddRange(GetInstalledProgramsFromRegistry(RegistryView.Registry32)); 
    result.AddRange(GetInstalledProgramsFromRegistry(RegistryView.Registry64)); 

    if (result != null && result.Count > 0) 
    { 
     // Convert to DataTable. 
     DataTable table = ConvertListToDataTable(result); 
     foreach (var column in table.Columns.Cast<DataColumn>().ToArray()) 
     { 
      if (table.AsEnumerable().All(dr => dr.IsNull(column))) 
       table.Columns.Remove(column); 
     } 
     table.Columns["Column1"].ColumnName = "Installed Software"; 
     GenerateExcel(table); 
    } 
} 


private DataTable ConvertListToDataTable(List<string> result) 
{ 
    // New table. 
    DataTable table = new DataTable(); 

    // Get max columns. 
    int columns = 0; 
    foreach (var array in result) 
    { 
     if (array.Length > columns) 
      columns = array.Length; 
    } 

    // Add columns. 
    for (int i = 0; i < columns; i++) 
     table.Columns.Add(); 

    // Add rows. 
    foreach (var array in result) 
     table.Rows.Add(array); 

    return table; 
} 

private static IEnumerable<string> GetInstalledProgramsFromRegistry(RegistryView registryView) 
{ 
    var result = new List<string>(); 

    using (RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView).OpenSubKey(registry_key)) 
    { 
     foreach (string subkey_name in key.GetSubKeyNames()) 
     { 
      using (RegistryKey subkey = key.OpenSubKey(subkey_name)) 
      { 
       if (IsProgramVisible(subkey)) 
       { 
        result.Add((string)subkey.GetValue("DisplayName")); 
       } 
      } 
     } 
    } 
    return result; 
} 

private static bool IsProgramVisible(RegistryKey subkey) 
{ 
    var name = (string)subkey.GetValue("DisplayName"); 
    var releaseType = (string)subkey.GetValue("ReleaseType"); 
    //var unistallString = (string)subkey.GetValue("UninstallString"); 
    var systemComponent = subkey.GetValue("SystemComponent"); 
    var parentName = (string)subkey.GetValue("ParentDisplayName"); 

    return !string.IsNullOrEmpty(name) && 
      string.IsNullOrEmpty(releaseType) && 
      string.IsNullOrEmpty(parentName) && 
      (systemComponent == null); 
} 

private void GenerateExcel(DataTable dt) 
{ 
    XL.Application oXL; 
    XL._Workbook oWB; 
    XL._Worksheet oSheet; 
    XL.Range oRng; 

    try 
    { 
     oXL = new XL.Application(); 
     Application.DoEvents(); 
     oXL.Visible = false; 
     //Get a new workbook. 
     oWB = (XL._Workbook)(oXL.Workbooks.Add(Missing.Value)); 
     oSheet = (XL._Worksheet)oWB.ActiveSheet; 
     //System.Data.DataTable dtGridData=ds.Tables[0]; 
     int iRow = 2; 
     if (dt.Rows.Count > 0) 
     { 
      for (int j = 0; j < dt.Columns.Count; j++) 
      { 
       oSheet.Cells[1, j + 1] = dt.Columns[j].ColumnName; 
      } 
      // For each row, print the values of each column. 
      for (int rowNo = 0; rowNo < dt.Rows.Count; rowNo++) 
      { 
       for (int colNo = 0; colNo < dt.Columns.Count; colNo++) 
       { 
        oSheet.Cells[iRow, colNo + 1] = dt.Rows[rowNo][colNo].ToString(); 
       } 
       iRow++; 
      } 
      iRow++; 
     } 
     oRng = oSheet.get_Range("A1", "IV1"); 
     oRng.EntireColumn.AutoFit(); 
     oXL.Visible = true; 
    } 
    catch (Exception theException) 
    { 
     throw theException; 
    } 
    finally 
    { 
     oXL = null; 
     oWB = null; 
     oSheet = null; 
     oRng = null; 
    } 
} 

預先感謝在這個問題尋找它,花費您寶貴的時間。

+0

是你的問題有關特定版本的C#?如果不是,你不需要使用'c#-4.0 c#-3.0 c#-2.0 c#-5.0'標籤.. –

+0

感謝@Soner引導me.My問題與任何特定版本的C#無關。爲了使這個問題可用於所有特定版本的C#用戶,我也做了.Apology並再次感謝。 –

+0

可能重複的[如何閱讀遠程註冊表鍵?](http://stackoverflow.com/questions/1566547/how-to-read-remote-registry-keys) – scheien

回答

2
private static List<string> ReadRemoteRegistryusingWMI(string machineName) 
     { 
     List<string> programs = new List<string>(); 

     ConnectionOptions connectionOptions = new ConnectionOptions(); 
     connectionOptions.Username = @"*******"; 
     connectionOptions.Password = "*******"; 
     //connectionOptions.Impersonation = ImpersonationLevel.Impersonate; 
     ManagementScope scope = new ManagementScope("\\\\" + machineName + "\\root\\CIMV2", connectionOptions); 
     scope.Connect(); 

     string softwareRegLoc = @"Software\Microsoft\Windows\CurrentVersion\Uninstall"; 

     ManagementClass registry = new ManagementClass(scope, new ManagementPath("StdRegProv"), null); 
     ManagementBaseObject inParams = registry.GetMethodParameters("EnumKey"); 
     inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE 
     inParams["sSubKeyName"] = softwareRegLoc; 

     // Read Registry Key Names 
     ManagementBaseObject outParams = registry.InvokeMethod("EnumKey", inParams, null); 
     string[] programGuids = outParams["sNames"] as string[]; 

     foreach (string subKeyName in programGuids) 
      { 
      inParams = registry.GetMethodParameters("GetStringValue"); 
      inParams["hDefKey"] = 0x80000002;//HKEY_LOCAL_MACHINE 
      inParams["sSubKeyName"] = softwareRegLoc + @"\" + subKeyName; 
      inParams["sValueName"] = "DisplayName"; 
      // Read Registry Value 
      outParams = registry.InvokeMethod("GetStringValue", inParams, null); 

      if (outParams.Properties["sValue"].Value != null) 
       { 
       string softwareName = outParams.Properties["sValue"].Value.ToString(); 
       programs.Add(softwareName); 
       } 
      } 

     return programs; 
     } 

從上面的代碼,現在我能夠列出安裝在遠程computer.Thanks大家的幫助和支持的所有軟件。

+0

請問你能告訴我什麼是機器名稱,用戶名和密碼? –

相關問題