【问题标题】:Getting Installed softwares from remote host using wmi使用 wmi 从远程主机获取安装的软件
【发布时间】:2011-12-19 10:48:46
【问题描述】:

我想检索从远程主机安装的软件。我想从注册表而不是 Win32_Product 中获取详细信息。我正在使用 wmi。我尝试了很多来自网络的例子。它们中的大多数都在我需要的 C# 中的 vb.net 中。任何人都可以发布代码..

这是我正在使用的代码

string regKeyToGet=@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\";
string keyToRead= "DisplayName"; 
ConnectionOptions oConn = new ConnectionOptions(); 
oConn.Username = "Ravinilson"; 
oConn.Password = "ravi"; 

ManagementScope scope = new ManagementScope(@"//" + RemotePC + @"/root/default",      oConn); 
ManagementClass registry = new ManagementClass(scope, new ManagementPath("StdRegProv"), null); 
// Returns a specific value for a specified key 
ManagementBaseObject inParams = registry.GetMethodParameters("GetStringValue"); 
nParams["sSubKeyName"] = regKeyToGet; 
inParams["sValueName"] = keyToRead; 
ManagementBaseObject outParams = registry.InvokeMethod("GetStringValue", inParams, null); 
return outParams["sValue"].ToString();

但它给出“对象引用未设置为对象的实例”错误。 我正在从“Win32_Product”获取已安装的应用程序。但它只返回 Windows 产品。这就是为什么我想从注册表“SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\”获取数据。

【问题讨论】:

  • 您已经在这里待了七个月,问了九个问题,从未接受过答案,从未提交过投票,而在这里,您基本上是在要求我们不费吹灰之力地为您提供代码。恐怕结局不会好……
  • 哦,对不起..如果我得到了你的答案,我只是使用它。我不知道我应该接受它。
  • 您到底为什么不想使用记录在案的方法呢?不要在注册表中寻找东西。将 VB.NET 代码转换为 C# 代码非常简单。在发布问题之前,您需要学会为自己付出一点努力。

标签: c# wmi


【解决方案1】:

我使用下面的脚本解决了这个问题。

#PRAGMA AUTORECOVER
[dynamic, provider("RegProv"),
ProviderClsid("{fe9af5c0-d3b6-11ce-a5b6-00aa00680c3f}"),   
ClassContext("local|HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows
\\CurrentVersion\\Uninstall")] 
class InstalledSoftware 
{
   [key] string KeyName;
   [read, propertycontext("DisplayName")]      string DisplayName;
   [read, propertycontext("DisplayVersion")]   string  DisplayVersion;
   [read, propertycontext("InstallDate")]      string InstallDate;
   [read, propertycontext("Publisher")]        string Publisher;
};

将上述脚本另存为“.mof”文件。之后,您需要使用命令提示符命令 "mofcomp filename.mof" 编译此脚本。为此,您需要具有管理员权限。编译文件后,上面的“InstalledSoftware”类将被添加到默认根目录下的 wmi 类中。

现在您将能够通过 wmi 使用类名“InstalledSoftware”访问该 PC 中已安装的应用程序。一件棘手的事情是,我们需要在您需要访问已安装软件的所有远程机器上编译上述脚本。

【讨论】:

    猜你喜欢
    • 2020-11-14
    • 1970-01-01
    • 2014-08-06
    • 2018-01-12
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 2019-05-17
    相关资源
    最近更新 更多