1

なぜ私のクエリ"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);
                    }

ご協力いただきありがとうございます

4

1 に答える 1

2

このコード:

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」と呼ばれていますね 私が私のものを「アルバート」と呼んだ場合:

var table = new DataTable("Albert");

私は得るColumn 'Name' does not belong to table Albert.

次のようなことをする必要があります。

var table = new DataTable("Albert");
table.Columns.Add("Name");
table.Columns.Add("ProcessID", typeof(int));
table.Columns.Add("Caption");
于 2011-06-06T08:50:02.423 に答える