How do you get the UserName of the owner of a process?

后端 未结 7 1671
迷失自我
迷失自我 2020-12-06 16:28

I\'m trying to get a list of processes currently owned by the current user (Environment.UserName). Unfortunately, the Process class doesn\'t have a

7条回答
  •  半阙折子戏
    2020-12-06 17:23

    Thanks, your answers put me on the proper path. For those who needs a code sample:

    public class App
    {
        public static void Main(string[] Args)
        {
            Management.ManagementObjectSearcher Processes = new Management.ManagementObjectSearcher("SELECT * FROM Win32_Process");
    
            foreach (Management.ManagementObject Process in Processes.Get()) {
                if (Process["ExecutablePath"] != null) {
                    string ExecutablePath = Process["ExecutablePath"].ToString();
    
                    string[] OwnerInfo = new string[2];
                    Process.InvokeMethod("GetOwner", (object[]) OwnerInfo);
    
                    Console.WriteLine(string.Format("{0}: {1}", IO.Path.GetFileName(ExecutablePath), OwnerInfo[0]));
                }
            }
    
            Console.ReadLine();
        }
    }
    

提交回复
热议问题