473,513 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UserName of a process

In asp.net(C#) we can get the Process Name of a process using the following
code:

System.Diagnostics.Process item;
item.ProcessName.ToString();

Is there a way to get the User Name of a process?
--
Thanks
Prisy
Nov 17 '05 #1
5 6307
I think you are looking for System.Environment.UserName.

Frisky

"Prisy" <Pr***@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
In asp.net(C#) we can get the Process Name of a process using the
following
code:

System.Diagnostics.Process item;
item.ProcessName.ToString();

Is there a way to get the User Name of a process?
--
Thanks
Prisy

Nov 17 '05 #2
Prisy,

In order to do that, you would have to get a handle to the process you
want to get the username for first. Assuming you have that, you will have
to do a bit of P/Invoke calls to get it.

First, you would call OpenProcessToken. Once you have the token for the
process, you would call GetTokenInformation passing the value TokenUser for
the TokenInformationClass parameter. This will return a TOKEN_USER
structure which contains the SID for the user that the process was launched
under. The only field that the TOKEN_USER structure has is of type
SID_AND_ATTRIBUTES, which has two fields, Sid and Attributes. The Sid field
is the pointer to the SID. With that, you can call the LookupAccountSid
function, passing the pointer to the SID and getting the domain and user
that the process was started under.

You should be able to find most of the definitions for these functions
at http://www.pinvoke.net. If they are not there, post back, and we can
help you with the definitions.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Prisy" <Pr***@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
In asp.net(C#) we can get the Process Name of a process using the
following
code:

System.Diagnostics.Process item;
item.ProcessName.ToString();

Is there a way to get the User Name of a process?
--
Thanks
Prisy

Nov 17 '05 #3
How can i check to see if the UserName of the process is
System.Environment.UserName?

--
Thanks
Prisy
"Frisky" wrote:
I think you are looking for System.Environment.UserName.

Frisky

"Prisy" <Pr***@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
In asp.net(C#) we can get the Process Name of a process using the
following
code:

System.Diagnostics.Process item;
item.ProcessName.ToString();

Is there a way to get the User Name of a process?
--
Thanks
Prisy


Nov 17 '05 #4

"Prisy" <Pr***@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
In asp.net(C#) we can get the Process Name of a process using the
following
code:

System.Diagnostics.Process item;
item.ProcessName.ToString();

Is there a way to get the User Name of a process?
--
Thanks
Prisy


Yep, using System.Management classes, here is how....

using System.Management;
using System.Diagnostics;
....
Process p = Process.GetCurrentProcess();
GetProcessIdentity(p.Id);
}

static void GetProcessIdentity(int Id)
{
using(ManagementObject process = new
ManagementObject("win32_process.handle=" + Id))
{
foreach (ManagementObject logonSession in
process.GetRelated("win32_logonSession"))
{
foreach(ManagementBaseObject account in
logonSession.GetRelated("win32_UserAccount"))
{
PropertyDataCollection processProperties = account.Properties;
Console.WriteLine("Name: {0} ,Domain: {1} ,Fullname: {2}, ,SID:
{3}" ,
processProperties["Name"].Value,
processProperties["Domain"].Value,
processProperties["FullName"].Value,
processProperties["SID"].Value);
}
}
}
}

Willy.
Nov 17 '05 #5
Nicholas,

I am trying to follow the steps you have mentioned. What do i have to import
in order to be able to use the methods like OpenProcessToken,
GetTokenInformation, etc...? I will be searching online as i wait for your
reply.

--
Thanks
Prisy
"Nicholas Paldino [.NET/C# MVP]" wrote:
Prisy,

In order to do that, you would have to get a handle to the process you
want to get the username for first. Assuming you have that, you will have
to do a bit of P/Invoke calls to get it.

First, you would call OpenProcessToken. Once you have the token for the
process, you would call GetTokenInformation passing the value TokenUser for
the TokenInformationClass parameter. This will return a TOKEN_USER
structure which contains the SID for the user that the process was launched
under. The only field that the TOKEN_USER structure has is of type
SID_AND_ATTRIBUTES, which has two fields, Sid and Attributes. The Sid field
is the pointer to the SID. With that, you can call the LookupAccountSid
function, passing the pointer to the SID and getting the domain and user
that the process was started under.

You should be able to find most of the definitions for these functions
at http://www.pinvoke.net. If they are not there, post back, and we can
help you with the definitions.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Prisy" <Pr***@discussions.microsoft.com> wrote in message
news:F9**********************************@microsof t.com...
In asp.net(C#) we can get the Process Name of a process using the
following
code:

System.Diagnostics.Process item;
item.ProcessName.ToString();

Is there a way to get the User Name of a process?
--
Thanks
Prisy


Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4238
by: William Stacey [MVP] | last post by:
Is this a bug in fx2? I expect they did not carry forward all the StartInfo properties to api used with Username/Password. When you remove UserName/password, the window does not show as expected....
0
7188
by: Paul | last post by:
Hi, I'm trying to kick off the iiscnfg.vbs from a webservice to export a website's config to an xml file (And eventually populate other servers with the config). I initially tried this using the...
8
6696
by: Philip Wagenaar | last post by:
I need to send printjobs to a printqueue under diffrent usernames. The printsoftware on the queue is not very 'secure' so I can create a user on 1 system and send a printjob under it's name to the...
1
5793
by: MS MVP ??? for VB.NET | last post by:
Dear all, I am trying to get the Process UserName by using System.Enviorment.Username, I have no problem by normal "run" and by "runas". I can get the process Username with correct value....
11
13321
by: Kirk | last post by:
The following C# web service works fine until you uncomment the lines setting UserName and Password. Then the process starts as the specified user, but hangs in a suspended state. In fact, any...
0
6647
by: Kirk | last post by:
The following C# web service works fine until you uncomment the lines setting UserName and Password. Then, Process.Start throws an Access is Denied Exception. This is with .NET 2.0, of course...
11
3609
by: gopal | last post by:
Hi, I am trying to call the OSQL utility from my C# console application and i am having problems i have the following code In Main method ProcessStartInfo psi = new...
7
2266
by: gopal | last post by:
Hi, I have a console application whose purpose is to run the OSQL utility from my console application. When my application is run, the OSQL utility is started and it has to prompt for...
6
6379
by: tsmojver | last post by:
Does anyone know how to retreive the logon account username for a particular windows service? Or the current user that is running this service as a process. C# is the prefered language of choice. ...
0
7264
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3236
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
805
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
459
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.