私のアカウントには管理者権限があります。次のように、PowerShellを使用してWindows 7EnterpriseVMでWMIにアクセスします。
Get-WmiObject -Namespace root\SecurityCenter2 -Class AntiVirusProduct -ComputerName $computername
C#では次のようになります。
string computer = Environment.MachineName;
string wmipath = @"\\" + computer + @"\root\SecurityCenter2";
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmipath,
"SELECT * FROM AntivirusProduct");
ManagementObjectCollection instances = searcher.Get();
//MessageBox.Show(instances.Count.ToString());
foreach (ManagementObject queryObj in instances)
{
return queryObj[type].ToString();
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
ただし、Powershellのコードは常に機能しますが、C#のコードは、プログラムを管理者として明示的に実行した場合にのみ機能します。管理者として明示的にC#プログラムを起動しなくても、管理者権限を持つユーザーに対して実行できるように、C#コードに何かを追加できますか?