Click to See Complete Forum and Search --> : [2.0] Process owner
francisstokes
Aug 12th, 2006, 09:31 AM
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.
ComputerJy
Aug 12th, 2006, 03:51 PM
I think it'sProcess1.StartInfo.UserName;
francisstokes
Aug 13th, 2006, 12:59 PM
Thanks for reply, but i had no luck with that. Anyone else?
Negative0
Aug 14th, 2006, 03:39 PM
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:
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());
francisstokes
Aug 18th, 2006, 01:47 PM
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:
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?
francisstokes
Aug 20th, 2006, 07:36 PM
bump
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.