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