How does a process find its user

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible in .NET2.0 to find the user that the current process is
running under without going through the whole WMI palaver? I tried looking at
ProcessStartInfo.UserName but that was empty - I presume that's more for new
starting processes than examining the current one.
 
Wow, brilliant, and it's not even new in VS2005!

Now - do you know how to get the user that a DIFFERENT process is running
under? I can do Process.GetProcesses() but a Process doesn't seem to include
user info.
 
Something like this ?

static void Main(string[] args) {
string[] arg = { "", "" };

ManagementScope scope = new ManagementScope(@"\root\cimv2");
ObjectQuery query = new ObjectQuery("select * from
Win32_Process");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query);
ManagementObjectCollection processes = searcher.Get();
foreach (ManagementObject process in processes) {
process.InvokeMethod("GetOwner", arg);
Console.WriteLine("caption = " + process["caption"] + "
owner = " + arg[1] + "\\" + arg[0]);
}
}

note you would just need the process id to only call it for a single process

Cheers,

Greg
 
Yes, that's the "WMI palaver" I mentioned earlier. I was hoping .NET2.0 might
have something more elegant. Thanks anyway.
--
Dave


Greg Young said:
Something like this ?

static void Main(string[] args) {
string[] arg = { "", "" };

ManagementScope scope = new ManagementScope(@"\root\cimv2");
ObjectQuery query = new ObjectQuery("select * from
Win32_Process");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query);
ManagementObjectCollection processes = searcher.Get();
foreach (ManagementObject process in processes) {
process.InvokeMethod("GetOwner", arg);
Console.WriteLine("caption = " + process["caption"] + "
owner = " + arg[1] + "\\" + arg[0]);
}
}

note you would just need the process id to only call it for a single process

Cheers,

Greg
 
There are other ways of getting the same information (such as pinvoking). I
am not sure if you consider those more elegant.

Cheers,

Greg
Dave said:
Yes, that's the "WMI palaver" I mentioned earlier. I was hoping .NET2.0
might
have something more elegant. Thanks anyway.
--
Dave


Greg Young said:
Something like this ?

static void Main(string[] args) {
string[] arg = { "", "" };

ManagementScope scope = new ManagementScope(@"\root\cimv2");
ObjectQuery query = new ObjectQuery("select * from
Win32_Process");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query);
ManagementObjectCollection processes = searcher.Get();
foreach (ManagementObject process in processes) {
process.InvokeMethod("GetOwner", arg);
Console.WriteLine("caption = " + process["caption"] + "
owner = " + arg[1] + "\\" + arg[0]);
}
}

note you would just need the process id to only call it for a single
process

Cheers,

Greg
Dave said:
Wow, brilliant, and it's not even new in VS2005!

Now - do you know how to get the user that a DIFFERENT process is
running
under? I can do Process.GetProcesses() but a Process doesn't seem to
include
user info.
--
Dave


:

WindowsPrincipal Principle = Thread.CurrentPrincipal as
WindowsPrincipal;

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

Is it possible in .NET2.0 to find the user that the current process
is
running under without going through the whole WMI palaver? I tried
looking
at
ProcessStartInfo.UserName but that was empty - I presume that's more
for
new
starting processes than examining the current one.
 
Hmm. Actually it's really speed that is the important factor - but I doubt
that a pinvoke will be significantly faster.
--
Dave


Greg Young said:
There are other ways of getting the same information (such as pinvoking). I
am not sure if you consider those more elegant.

Cheers,

Greg
Dave said:
Yes, that's the "WMI palaver" I mentioned earlier. I was hoping .NET2.0
might
have something more elegant. Thanks anyway.
--
Dave


Greg Young said:
Something like this ?

static void Main(string[] args) {
string[] arg = { "", "" };

ManagementScope scope = new ManagementScope(@"\root\cimv2");
ObjectQuery query = new ObjectQuery("select * from
Win32_Process");
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(scope, query);
ManagementObjectCollection processes = searcher.Get();
foreach (ManagementObject process in processes) {
process.InvokeMethod("GetOwner", arg);
Console.WriteLine("caption = " + process["caption"] + "
owner = " + arg[1] + "\\" + arg[0]);
}
}

note you would just need the process id to only call it for a single
process

Cheers,

Greg
Wow, brilliant, and it's not even new in VS2005!

Now - do you know how to get the user that a DIFFERENT process is
running
under? I can do Process.GetProcesses() but a Process doesn't seem to
include
user info.
--
Dave


:

WindowsPrincipal Principle = Thread.CurrentPrincipal as
WindowsPrincipal;

Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

Is it possible in .NET2.0 to find the user that the current process
is
running under without going through the whole WMI palaver? I tried
looking
at
ProcessStartInfo.UserName but that was empty - I presume that's more
for
new
starting processes than examining the current one.
 
I just tried this, and it doesn't actually work. For one thing
Thread.CurrentPrinciple actually returns a GenericPrincipal not a
WindowsPrincipal, and secondly Thread.CurrentPrincipal.Identity.Name, which
presumably should contain the user name, is blank. Maybe I'm doing something
wrong. Anyway, I'm now using WindowsIdentity.GetCurrent().Name which seems to
work OK.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top