如何使用ManagementObjectSearcher? [英] How to use ManagementObjectSearcher?

查看:73
本文介绍了如何使用ManagementObjectSearcher?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.net 2010
现在,我试图从流程ID中获取用户.代码是:

I am using .net 2010
Now I am trying to get the user if from a process id. The code is:

using System;
using System.Net;
using System.Collections;
//using System.Net.NetworkInformation;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Management;
using System.Security.Permissions;
using System.Security.Principal;
using System.ComponentModel.Composition;
using System.ComponentModel;
using System.Diagnostics;


namespace getUser
{
    public static class Utils
    {
        public const int NO_ERROR = 0;
        public const int MIB_TCP_STATE_CLOSED = 1;
        public const int MIB_TCP_STATE_LISTEN = 2;
        public const int MIB_TCP_STATE_SYN_SENT = 3;
        public const int MIB_TCP_STATE_SYN_RCVD = 4;
        public const int MIB_TCP_STATE_ESTAB = 5;
        public const int MIB_TCP_STATE_FIN_WAIT1 = 6;
        public const int MIB_TCP_STATE_FIN_WAIT2 = 7;
        public const int MIB_TCP_STATE_CLOSE_WAIT = 8;
        public const int MIB_TCP_STATE_CLOSING = 9;
        public const int MIB_TCP_STATE_LAST_ACK = 10;
        public const int MIB_TCP_STATE_TIME_WAIT = 11;
        public const int MIB_TCP_STATE_DELETE_TCB = 12;
        //#region helper function
        const int MAXSIZE = 16384; // size _does_ matter
        public static string GetProcessInfoByPID(int PID, out string User, out string Domain)//, out string OwnerSID)
        {
            //DataTable dt = new DataTable();
            //dt.Columns.Add("ProcessID");
            //dt.Columns.Add("Name");
            //dt.Columns.Add("Description");
            //dt.Columns.Add("User");
            //dt.Columns.Add("Domain");
            //dt.Columns.Add("OwnerSID");
            User = String.Empty;
            Domain = String.Empty;
            String OwnerSID = String.Empty;
            string processname = String.Empty;
            try
            {
                ObjectQuery sq = new ObjectQuery("Select * from Win32_Process Where ProcessID = '" + PID + "'");
                //ObjectQuery sq = new ObjectQuery(Query);
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(sq);
                if (searcher.Get().Count == 0)
                    return "Unknown";
                foreach (ManagementObject oReturn in searcher.Get())
                {
                    //Name of process
                    //arg to send with method invoke to return user and domain - below is link to SDK doc on it
                    string[] o = new String[2];
                    //Invoke the method and populate the o var with the user name and domain
                    oReturn.InvokeMethod("GetOwner", (object[])o);
                    //int pid = (int)oReturn["ProcessID"];
                    processname = (string)oReturn["Name"];
                    //dr[2] = oReturn["Description"];
                    User = o[0];
                    if (User == null)
                        User = String.Empty;
                    Domain = o[1];
                    if (Domain == null)
                        Domain = String.Empty;
                    string[] sid = new String[1];
                    oReturn.InvokeMethod("GetOwnerSid", (object[])sid);
                    OwnerSID = sid[0];
                    return OwnerSID;
                }
            }
            catch
            {
                return OwnerSID;
            }
            return OwnerSID;
        }
    }
}




现在我收到此错误:




Now I get this error:

Error	3	The type 'System.ComponentModel.Component' is defined in an assembly that is not referenced. You must add a reference to assembly 'System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.	C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\getUser\getprocessownersid.cs	54	17	getUser





谁能告诉我该怎么办?





Can anyone tell me what to do?

推荐答案



请参阅此示例.我使用传递应用程序名称而不是ProcessID进行查询.

Hi,

See this sample. I use to pass Apps Name rather than ProcessID for query.

   using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Management;
using System.Text;
public partial class ProcessId : System.Web.UI.Page
{
       
    protected void Button1_Click(object sender, EventArgs e)
    {
        string PID = "Outlook.exe";
        string User = string.Empty;
        string Domain = string.Empty;
        var processId = GetProcessInfoByPID(PID, User, Domain);
    }
    private object GetProcessInfoByPID(string PID, string User, string Domain) //, out string OwnerSID)
    {
        string  OwnerSID = string.Empty;
        ConnectionOptions connection = new ConnectionOptions();
        ConnectionOptions options = new ConnectionOptions();
        string targetIpAddress = "210.0.0.000";   // Pass a valid IP address (sample only)
        connection.Authentication = System.Management.AuthenticationLevel.Packet;
        ManagementScope scope = new ManagementScope(("\\\\" + targetIpAddress), options);
        scope.Connect();
        ManagementPath p = new ManagementPath("Win32_Product");
        ManagementClass classInstance = new ManagementClass(scope, p, null);
        ObjectQuery query = new ObjectQuery("Select * from Win32_Process Where Name = '" + PID + "'");
        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
        foreach (ManagementObject oReturn in searcher.Get())
        {
            //Name of process
            //arg to send with method invoke to return user and domain - below is link to SDK doc on it
            string[] o = new String[2];
            //Invoke the method and populate the o var with the user name and domain
            oReturn.InvokeMethod("GetOwner", (object[])o);
            var pid = oReturn["ProcessID"];
            var processname = (string)oReturn["Name"];
            //dr[2] = oReturn["Description"];
            User = o[0];
            if (User == null)
                User = String.Empty;
            Domain = o[1];
            if (Domain == null)
                Domain = String.Empty;
            string[] sid = new String[1];
            oReturn.InvokeMethod("GetOwnerSid", (object[])sid);
            OwnerSID = sid[0];
            return OwnerSID;
        }
        return OwnerSID;
    }
}



请记住,如果有帮助,请将其标记为答案;如果没有帮助,则将其取消标记.

问候,

代数



Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Regards,

Algem


这篇关于如何使用ManagementObjectSearcher?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆