查询在 C# 上不起作用,但在 powershell 上起作用

发布于 2024-11-13 06:56:55 字数 1651 浏览 3 评论 0原文

任何人都知道为什么我的查询 "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2024-11-20 06:56:55

这段代码:

const string queryString = "SELECT Name, ProcessID, Caption FROM Win32_Process";

var scope = new ManagementScope(@"\root\cimv2");
var query = new ObjectQuery(queryString);
var searcher = new ManagementObjectSearcher(scope, query);
var objectCollection = searcher.Get();

foreach (var o in objectCollection)
{
    Console.WriteLine("{0} {1} {2}", o["Name"], o["ProcessID"], o["Caption"]);
}

...对我来说效果很好。您的代码到底如何不起作用?

(顺便说一句,您似乎没有对 options 执行任何操作)。

更新:

它实际上是在抱怨,因为您没有在DataTable中设置列。我认为你把你的例子删减太多了。您的DataTable 名为“Win32_Process”,是吗?如果我称我的为“Albert”:

var table = new DataTable("Albert");

我得到列“名称”不属于表 Albert。

您需要执行如下操作:

var table = new DataTable("Albert");
table.Columns.Add("Name");
table.Columns.Add("ProcessID", typeof(int));
table.Columns.Add("Caption");

This code:

const string queryString = "SELECT Name, ProcessID, Caption FROM Win32_Process";

var scope = new ManagementScope(@"\root\cimv2");
var query = new ObjectQuery(queryString);
var searcher = new ManagementObjectSearcher(scope, query);
var objectCollection = searcher.Get();

foreach (var o in objectCollection)
{
    Console.WriteLine("{0} {1} {2}", o["Name"], o["ProcessID"], o["Caption"]);
}

...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. Your DataTable is called "Win32_Process", yes? If I call mine "Albert":

var table = new DataTable("Albert");

I get Column 'Name' does not belong to table Albert.

You need to do something like the following:

var table = new DataTable("Albert");
table.Columns.Add("Name");
table.Columns.Add("ProcessID", typeof(int));
table.Columns.Add("Caption");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文