2011-05-11 129 views
1

我正在開發一個C#程序以遠程卸載應用程序。它工作正常,但問題是它沒有列出特定選定計算機上的所有已安裝產品。以靜默方式卸載遠程計算機上的應用程序

上市使用WMI安裝的產品的代碼爲:

void ListAllProducts() 
{ 
    try 
    { 
     ConnectionOptions connection = new ConnectionOptions(); 
     connection.Username = Connect.UserName; 
     connection.Password = Connect.Password; 
     connection.Authority = "ntlmdomain:MSHOME"; 

     ManagementScope scope = new ManagementScope("\\\\"+ Connect.MachineName +"\\root\\CIMV2", connection); 
     scope.Connect(); 

     ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Product"); 

     ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); 
     System.Threading.Thread.Sleep(5000); 

     foreach (ManagementObject queryObj in searcher.Get()) 
     { 
      listBox4.Items.Add(queryObj["Name"].ToString()); 
      listBox2.Items.Add (queryObj["Name"].ToString()); 
      listBox1.Items.Add(queryObj["IdentifyingNumber"].ToString()); 
      listBox3.Items.Add(queryObj["Version"].ToString()); 
     } 
    } 
    catch (ManagementException e) 
    { 
     MessageBox.Show("An error occurred while querying for WMI data: " + e.Message); 
    } 
} 

卸載所有產品的代碼是:

void UninstallProduct() 
{ 
    try 
    { 
     ConnectionOptions connection = new ConnectionOptions(); 
     connection.Username = Connect.UserName; 
     connection.Password = Connect.Password; 
     connection.Authority = "ntlmdomain:MSHOME"; 

     ManagementScope scope = new ManagementScope("\\\\"+Connect.MachineName +"\\root\\CIMV2", connection); 
     scope.Connect(); 

     ManagementObject classInstance = new ManagementObject(scope, new ManagementPath ("Win32_Product.IdentifyingNumber='"+listBox1.Text +"',Name='"+listBox2.Text+"',Version='"+ listBox3.Text+"'"),null); 

     // no method in-parameters to define 

     // Execute the method and obtain the return values. 
     ManagementBaseObject outParams = 
      classInstance.InvokeMethod("Uninstall", null, null); 

     // List outParams 
     MessageBox.Show ("Uninstallation Starts"); 
    } 
    catch(ManagementException err) 
    { 
     MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message); 
    } 
} 

請幫我列出所有安裝在所選擇的產品並在未經所選機器的用戶同意的情況下將其卸載。

+1

我需要你閱讀問題丟回給自己,並想一分鐘是否可能會有一個API來做到這一點。 – 2011-05-11 04:12:04

回答

1

的WMI Win32_Product僅代表已安裝的產品由Windows安裝程序。要獲得所有已安裝產品的列表,您需要枚舉SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall註冊表項的子項。要遠程執行此操作,可以使用WMI註冊表類StdRegProv類。的TechNet包括顯示如何可以做到這一點的示例腳本,以及可以適應您的特定需求:

How do I list all the installed applications on a given machine?
List Installed Software

相關問題