首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >静默卸载远程计算机上的应用程序

静默卸载远程计算机上的应用程序
EN

Stack Overflow用户
提问于 2011-05-11 12:08:11
回答 2查看 3.6K关注 0票数 1

我正在开发一个C#程序来远程卸载应用程序。它工作得很好,但问题是它没有列出特定选定计算机上安装的所有产品。

使用WMI列出已安装产品的代码为:

代码语言:javascript
复制
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);
    }
}

卸载所有产品的代码为:

代码语言:javascript
复制
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);
    }
}

请帮助我列出所选计算机上安装的所有产品,并在未经所选计算机的用户同意的情况下卸载它。

EN

回答 2

Stack Overflow用户

发布于 2011-05-11 12:14:34

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

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

票数 1
EN

Stack Overflow用户

发布于 2011-05-11 15:23:11

Windows 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5959127

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档