1
谁知道为什么我的查询"select Name, ProcessID, Caption from Win32_Process where ProcessId='" + processIds[index] + "'"
在我的程序C#返回查询不能在C#中工作,但在PowerShell中工作
Column 'Name' does not belong to table Win32_Process
。
在PowerShell中当我执行
Get-WmiObject -query "select Name, ProcessID, Caption from win32_process"
它的作品!
String queryString = "select Name, ProcessID, Caption from Win32_Process where ProcessId='" + processIds[index] + "'";
SelectQuery query = new SelectQuery(queryString);
ConnectionOptions options = new ConnectionOptions();
options.Authentication = System.Management.AuthenticationLevel.PacketPrivacy;
ManagementScope scope = new System.Management.ManagementScope("\\root\\cimv2");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
try
{
ManagementObjectCollection processes = searcher.Get();
DataTable result = new DataTable();
foreach (ManagementObject mo in processes)
{
DataRow row = result.NewRow();
if (mo["Name"] != null)
row["Name"] = mo["Name"].ToString();
row["ProcessId"] = Convert.ToInt32(mo["ProcessId"]);
if (mo["Caption"] != null)
row["Caption"] = mo["Caption"].ToString();
result.Rows.Add(row);
}
感谢您的帮助
嗨,我改变我的代码,我现在没有现货在Datarow中运行。 我想我有问题,当我尝试行[“名称”] =莫[“名称”]。你知道为什么吗 ? – 2011-06-06 08:57:12
查看我的更新;你没有添加任何列到你的'DataTable'中。 – 2011-06-06 08:57:47
好吧我现在明白了,非常感谢你。你拯救了我的生命! – 2011-06-06 08:59:16