查找远程系统中是否存在特定产品
尝试查找远程PC上是否安装了该产品,执行时间较长, 有什么方法可以让我们更快地实现这一目标吗?
//用法: //uninstall4("主机名", "产品名", "{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");
}
}
}
}
Trying to find the product is installed in remote pC or not, it takes long time to execute,
is there any ways we can achieve this faster.
//usage:
//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");
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有。 Win32_Product 类的提供程序必须在您查询时动态构建信息,而且它总是非常慢。
Nope. The provider for the Win32_Product class has to build the information dynamically when you query it, and it's always, always slow.