查询在 C# 上不起作用,但在 powershell 上起作用
任何人都知道为什么我的查询 "select Name, ProcessID, Caption from Win32_Process where ProcessId='" + processIds[index] + "'"
返回到
Column 'Name' does not belong to table Win32_Process
我的程序 C# 中。
在 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);
}
感谢您的帮助
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这段代码:
...对我来说效果很好。您的代码到底如何不起作用?
(顺便说一句,您似乎没有对
options
执行任何操作)。更新:
它实际上是在抱怨,因为您没有在
DataTable
中设置列。我认为你把你的例子删减太多了。您的DataTable
名为“Win32_Process”,是吗?如果我称我的为“Albert”:我得到
列“名称”不属于表 Albert。
您需要执行如下操作:
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. YourDataTable
is 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: