C# 终止进程需要WQL“;选择*.quot;?

C# 终止进程需要WQL“;选择*.quot;?,c#,.net,wmi,wql,C#,.net,Wmi,Wql,我正在编写代码,以便在指定的时间后终止特定进程。我正在使用以下代码(简化后): 问题在于,使用SELECT Name的WQL语句,CreationDate在尝试执行以下操作时引发异常: "Operation is not valid due to the current state of the object." …但是,使用SELECT*可以工作并终止进程。为什么--结果集中是否需要特定的WMI列 谢谢 当执行WMI方法时,将执行WMI内部searh,以便通过该方法识别实例 在这种情况下,对

我正在编写代码,以便在指定的时间后终止特定进程。我正在使用以下代码(简化后):

问题在于,使用
SELECT Name的WQL语句,CreationDate
在尝试执行以下操作时引发异常:

"Operation is not valid due to the current state of the object."
…但是,使用
SELECT*
可以工作并终止进程。为什么--结果集中是否需要特定的WMI列


谢谢

当执行WMI方法时,将执行WMI内部searh,以便通过该方法识别实例

在这种情况下,对于WMI类,WMI对象路径类似于Win32_Process.Handle=“8112”,因此正如您所看到的,
Handle
属性是WMI对象路径的一部分,必须包含在WQL语句中

检查一下这个样品

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
//this will all the notepad running instances

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                string ComputerName = "localhost";
                ManagementScope Scope;                

                if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)) 
                {
                    ConnectionOptions Conn = new ConnectionOptions();
                    Conn.Username  = "";
                    Conn.Password  = "";
                    Conn.Authority = "ntlmdomain:DOMAIN";
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
                }
                else
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT Handle FROM Win32_Process Where Name='notepad.exe'");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    WmiObject.InvokeMethod("Terminate", null);

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}

执行WMI方法时,将执行WMI内部searh,以便通过该方法标识实例

在这种情况下,对于WMI类,WMI对象路径类似于Win32_Process.Handle=“8112”,因此正如您所看到的,
Handle
属性是WMI对象路径的一部分,必须包含在WQL语句中

检查一下这个样品

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
//this will all the notepad running instances

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                string ComputerName = "localhost";
                ManagementScope Scope;                

                if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)) 
                {
                    ConnectionOptions Conn = new ConnectionOptions();
                    Conn.Username  = "";
                    Conn.Password  = "";
                    Conn.Authority = "ntlmdomain:DOMAIN";
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
                }
                else
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT Handle FROM Win32_Process Where Name='notepad.exe'");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    WmiObject.InvokeMethod("Terminate", null);

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}
你是说“选择句柄,名称,创建日期”是需要的吗?我刚刚测试过——是的,只需将句柄添加到WQL就可以实现终止。谢谢你是说“选择句柄,名称,创建日期”是需要的吗?我刚刚测试过——是的,只需将句柄添加到WQL就可以实现终止。谢谢