【问题标题】:Uninstall application on remote computer silently静默卸载远程计算机上的应用程序
【发布时间】:2023-04-09 07:13:01
【问题描述】:

我正在开发一个 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);
    }
}

请帮我列出选定机器上安装的所有产品,并在未经选定机器用户同意的情况下将其卸载。

【问题讨论】:

  • 我需要你把这个问题读给自己听,想一想是否有可能有一个 API 可以做到这一点。

标签: c# wmi uninstallation


【解决方案1】:

我相信您的问题与了解远程计算机上安装了哪些应用程序有关。一旦你知道了,你就可以使用你的代码来卸载它们。在这种情况下,这里有一篇文章的链接,该链接介绍了如何列出远程计算机上的所有应用程序(及其卸载信息):

http://mdb-blog.blogspot.com/2010/12/c-check-if-programapplication-is.html

【讨论】:

    【解决方案2】:

    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

    【讨论】:

      猜你喜欢
      • 2011-06-26
      • 2011-09-11
      • 1970-01-01
      • 2015-06-04
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 2012-06-09
      相关资源
      最近更新 更多