Anyone know why my query "select Name, ProcessID, Caption from Win32_Process where ProcessId='" + processIds[index] + "'" return
Column 'Name' does not belong to table Win32_Process
in my program C#.
In powershell when I execute
Get-WmiObject -query "select Name, ProcessID, Caption from win32_process"
It’s works !
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);
}
Thanks for your help
This code:
…works fine for me. Exactly how is your code not working?
(You don’t appear to be doing anything with
options, by the way).Update:
It’s actually complaining because you’ve not set up the columns in your
DataTable. I think that you’ve cut down your example too much. YourDataTableis called “Win32_Process”, yes? If I call mine “Albert”:I get
Column 'Name' does not belong to table Albert.You need to do something like the following: