Results 1 to 6 of 6

Thread: [2.0] Process owner

  1. #1

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    [2.0] Process owner

    I've been using the Process class to get information about different processes, but I can't get the process owner. I've looked on MSDN about processes but i couldn't find anything. Is there a way to do this through API?

    Sorry if this question has been asked before.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: [2.0] Process owner

    I think it's
    Code:
    Process1.StartInfo.UserName;
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: [2.0] Process owner

    Thanks for reply, but i had no luck with that. Anyone else?

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2.0] Process owner

    I don't have .Net 2.0 on this machine, but my method should still work. You will need to add a reference to System.Management to your project:

    Code:
                StringBuilder objSB = new StringBuilder();
    
                string[] s= new string[2];
       
                //Use WMI to get the process info
                ManagementClass objMan = new ManagementClass ( @"root\cimv2:Win32_Process") ;
    
                //Loop through each process
                foreach ( ManagementObject objManOb in objMan.GetInstances() )
                {
                    //Call the get owner method, which gets us the username and domain of the user that started the process
                    objManOb.InvokeMethod("GetOwner",(object[])s);
    
                    objSB.Append (objManOb["Name"] + " - " + s[1] + "\\" + s[0] + Environment.NewLine );
                }
    
                MessageBox.Show(objSB.ToString());

  5. #5

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: [2.0] Process owner

    Im looking at that and thinking it should work, and when i tested that code, it did. But i need to get the owner of individual processes. So i tried to modify it a little, but no luck. Heres the code ive used:

    Code:
            private void button1_Click(object sender, EventArgs e)
            {
    
                try
    	    {
    		textbox1.Text = GetOwner(listBox1.SelectedItem);
                    
                }
                catch { MessageBox.Show("Error. No Process Selected?"); }
            }
    
            string GetOwner(string ProcessName)
            {
                StringBuilder objSB = new StringBuilder();
    
                string[] s = new string[2];
    
                //Use WMI to get the process info
                ManagementClass objMan = new ManagementClass(@"root\cimv2:Win32_Process");
    
                //Loop through each process and check name against WMI name
                foreach (ManagementObject objManOb in objMan.GetInstances())
                {
    
                    objManOb.InvokeMethod("GetOwner", (object[])s);
    
                    if (objManOb["Name"].ToString() == ProcessName)
                    {
                        objSB.Append(s[0]);
                    }
                }
    
                return objSB.ToString();
            }
    Its not returning errors, just nothing is showing up in the textbox. help?

  6. #6

    Thread Starter
    Hyperactive Member francisstokes's Avatar
    Join Date
    May 2005
    Location
    Kent, England
    Posts
    272

    Re: [2.0] Process owner

    bump

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width