<si***********@gmail.comwrote in message
news:11*********************@b75g2000hsg.googlegro ups.com...
>I need to obtain the username of the currently logged in user on a
machine. However, the program that needs to do this will be running
under different credentials to the logged in user, so using
Environment.UserName will give the user name of the user that is
running the program.
Any help would be appreciated, this is giving me a headache.
Using System.Management you can get at the parent user ID, of course this requires that your
process is spawned from the interactive logon session, for instance the explorer shell or
the command line shell.
Following method gets the current process -parent process -associated logon session ->
LogonUser.
static string GetParentUser(int pid)
{
string parentUserAccount = null;
string queryString = String.Format("select ParentProcessId from win32_process where
ProcessId={0}", pid);
using(ManagementObjectSearcher query = new ManagementObjectSearcher(new
SelectQuery(queryString))) {
foreach( ManagementObject mo in query.Get()) {
uint parentPid = (uint)mo.Properties["ParentProcessId"].Value;
queryString = String.Format("select Handle from win32_process where ParentProcessId =
{0}", parentPid);
using(ManagementObjectSearcher subQuery = new ManagementObjectSearcher(new
SelectQuery(queryString))) {
foreach( ManagementObject mo1 in subQuery.Get()) {
string handle = (string)mo1.Properties["Handle"].Value;
RelatedObjectQuery relatedQuery =
new RelatedObjectQuery ("associators of {Win32_Process.Handle=\"" + handle + "\"}");
relatedQuery.RelatedClass = "Win32_LogonSession";
using(ManagementObjectSearcher relQuery = new ManagementObjectSearcher(relatedQuery))
{
foreach( ManagementObject mo2 in relQuery.Get()) {
RelatedObjectQuery relQuery2 =
new RelatedObjectQuery ("associators of {Win32_LogonSession.LogonId='" +
mo2["LogonId"]+ "'}");
relQuery2.RelationshipClass = "win32_LoggedonUser";
using(ManagementObjectSearcher searcher2 = new ManagementObjectSearcher(relQuery2))
{
foreach (ManagementObject mo3 in searcher2.Get()) {
parentUserAccount = String.Format(@"{0}\{1}", mo3["Domain"], mo3["Name"]);
}
}
}
}
}
}
}
}
return parentUserAccount;
}
// usage...
..
Process proc = System.Diagnostics.Process.GetCurrentProcess();
string parentId = GetParentUser(proc.Id));
..
Note that you can call this at any time in a spawned process, however, if you are
impersonating, you have to call this method (GetParentUser) before impersonating.
Note that Vista uses plit tokens when running non elevated with UAC enabled, so you'll get
two times the same account back, this is dealt with by :
parentUserAccount += String.Format(@"[{0}\{1}]", mo3["Domain"], mo3["Name"]);
Willy.