找到特定产品是否存在于远程系统中

时间:2008-11-19 14:02:29

标签: wmi

尝试查找产品是否安装在远程pC中,执行需要很长时间, 我们有什么方法可以更快地实现这一目标。

//用法:         // uninstall4(“hostname”,“productname”,“{AC9C1263-2BA8-4863-BE18-01232375CE42}”,“10.0.0.69”);

    public void uninstall4(string targetServer, string product,string guid , string version)
    {
        //Connect to Server
        System.Management.ConnectionOptions connoptions = new System.Management.ConnectionOptions();
        connoptions.Impersonation = System.Management.ImpersonationLevel.Impersonate;
        connoptions.Timeout = new TimeSpan(0, 0, 10); // 10 seconds
        System.Management.ManagementScope scope = new System.Management.ManagementScope(@"\\" + targetServer + @"\root\cimv2", connoptions);
        scope.Connect();

        string q = "select * from Win32_Product where name = '" + product + "' and IdentifyingNumber='"+guid+"' and version = '"+version+"'";

        System.Management.SelectQuery query = new System.Management.SelectQuery(q);

        System.Management.EnumerationOptions options = new System.Management.EnumerationOptions();
        options.EnumerateDeep = false;
        options.ReturnImmediately = false;
        options.DirectRead = true;

        using (System.Management.ManagementObjectSearcher searcher
            = new System.Management.ManagementObjectSearcher(scope, query, options))
        {
            using (System.Management.ManagementObjectCollection moc = searcher.Get())
            {
                if(moc == null || moc.Count == 0)
                {
                    throw new Exception("product Not Found");
                }

            }
        }

    }

1 个答案:

答案 0 :(得分:2)

不。 Win32_Product类的提供程序必须在查询时动态地构建信息,并且它总是很慢。