469,323 Members | 1,238 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

How does a process find its user

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.
--
Dave
Jul 7 '06 #1
7 2910
WindowsPrincipal Principle = Thread.CurrentPrincipal as WindowsPrincipal;

Cheers,

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

"Dave" <Da**@discussions.microsoft.comwrote in message
news:2D**********************************@microsof t.com...
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.
--
Dave

Jul 7 '06 #2
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
"Greg Young" wrote:
WindowsPrincipal Principle = Thread.CurrentPrincipal as WindowsPrincipal;

Cheers,

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

"Dave" <Da**@discussions.microsoft.comwrote in message
news:2D**********************************@microsof t.com...
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.
--
Dave


Jul 7 '06 #3
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" <Da**@discussions.microsoft.comwrote in message
news:3A**********************************@microsof t.com...
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
"Greg Young" wrote:
>WindowsPrincipal Principle = Thread.CurrentPrincipal as WindowsPrincipal;

Cheers,

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

"Dave" <Da**@discussions.microsoft.comwrote in message
news:2D**********************************@microso ft.com...
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.
--
Dave



Jul 7 '06 #4
Yes, that's the "WMI palaver" I mentioned earlier. I was hoping .NET2.0 might
have something more elegant. Thanks anyway.
--
Dave
"Greg Young" wrote:
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" <Da**@discussions.microsoft.comwrote in message
news:3A**********************************@microsof t.com...
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
"Greg Young" wrote:
WindowsPrincipal Principle = Thread.CurrentPrincipal as WindowsPrincipal;

Cheers,

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

"Dave" <Da**@discussions.microsoft.comwrote in message
news:2D**********************************@microsof t.com...
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.
--
Dave


Jul 8 '06 #5
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" <Da**@discussions.microsoft.comwrote in message
news:DA**********************************@microsof t.com...
Yes, that's the "WMI palaver" I mentioned earlier. I was hoping .NET2.0
might
have something more elegant. Thanks anyway.
--
Dave
"Greg Young" wrote:
>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" <Da**@discussions.microsoft.comwrote in message
news:3A**********************************@microso ft.com...
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
"Greg Young" wrote:

WindowsPrincipal Principle = Thread.CurrentPrincipal as
WindowsPrincipal;

Cheers,

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

"Dave" <Da**@discussions.microsoft.comwrote in message
news:2D**********************************@microso ft.com...
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.
--
Dave



Jul 8 '06 #6
Hmm. Actually it's really speed that is the important factor - but I doubt
that a pinvoke will be significantly faster.
--
Dave
"Greg Young" wrote:
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" <Da**@discussions.microsoft.comwrote in message
news:DA**********************************@microsof t.com...
Yes, that's the "WMI palaver" I mentioned earlier. I was hoping .NET2.0
might
have something more elegant. Thanks anyway.
--
Dave
"Greg Young" wrote:
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" <Da**@discussions.microsoft.comwrote in message
news:3A**********************************@microsof t.com...
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
"Greg Young" wrote:

WindowsPrincipal Principle = Thread.CurrentPrincipal as
WindowsPrincipal;

Cheers,

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

"Dave" <Da**@discussions.microsoft.comwrote in message
news:2D**********************************@microsof t.com...
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.
--
Dave



Jul 8 '06 #7
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.
--
Dave
"Greg Young" wrote:
WindowsPrincipal Principle = Thread.CurrentPrincipal as WindowsPrincipal;

Cheers,

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

"Dave" <Da**@discussions.microsoft.comwrote in message
news:2D**********************************@microsof t.com...
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.
--
Dave


Jul 12 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Andrus | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.