我正在开发一个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);
}
}
请帮我列出所选机器上安装的所有产品,并在未经所选机器用户同意的情况下将其卸载。
答案 0 :(得分:1)
我相信您的问题涉及了解哪些应用程序安装在远程计算机上。一旦你知道,你可以使用你的代码卸载它们。在这种情况下,这里是一篇关于如何在远程计算机上列出所有应用程序(及其卸载信息)的文章的链接:
http://mdb-blog.blogspot.com/2010/12/c-check-if-programapplication-is.html
答案 1 :(得分:1)
WMI Win32_Product
仅代表Windows Installer安装的产品。要获取所有已安装产品的列表,您需要枚举SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
注册表项的子项。要远程执行此操作,可以使用WMI注册表类StdRegProv
类。 TechNet包含示例脚本,说明如何完成此任务,以及哪些可以适应您的特定需求:
How do I list all the installed applications on a given machine?
List Installed Software