laatkeur
asked on
Get the username of a process
Hi, is there a way in VB.Net to get the username of a process (like in Windows Task manager) ?
I don't want my application to run more than once by a single user on a machine, but more than one user can run it on the same machine at the same time.
So, I have no problem to detect that the process runs more than once, but I have to check that it's not runned by the same user.
Thank you
I don't want my application to run more than once by a single user on a machine, but more than one user can run it on the same machine at the same time.
So, I have no problem to detect that the process runs more than once, but I have to check that it's not runned by the same user.
Thank you
Have you tried using System.Environment.UserNam e for comparison? Or is your problem more complicated than that? Otherwise you might have to use some native calls...please post back if that's the situation...
See:
SystemInformation.UserName
Gets the user name for the current thread (the name of the user currently logged on to the operating system).
[Visual Basic]
Public Shared ReadOnly Property UserName As String
[C#]
public static string UserName {get;}
[C++]
public: __property static String* get_UserName();
[JScript]
public static function get UserName() : String;
Property Value
The name of the user currently logged on to the operating system.
SystemInformation.UserName
Gets the user name for the current thread (the name of the user currently logged on to the operating system).
[Visual Basic]
Public Shared ReadOnly Property UserName As String
[C#]
public static string UserName {get;}
[C++]
public: __property static String* get_UserName();
[JScript]
public static function get UserName() : String;
Property Value
The name of the user currently logged on to the operating system.
ASKER
No, I want to know the username of all processes of my application.
Suppose the user "Administrator" starts an instance of Application.exe. Then, another user (username "Blah") logs onto the same machine with Remote Desktop Connection and starts another instance of Application.exe.
When "Blah" open Application.exe, I want this instance to know that the first instance is used by "Administrator". If Blah opens Application.exe twice, I want the second instance to end right away (I already do that), but I don't want to end Blah's instance if the first one is used by another user.
Is it clear ? (I'm not that good in English, sorry)
Suppose the user "Administrator" starts an instance of Application.exe. Then, another user (username "Blah") logs onto the same machine with Remote Desktop Connection and starts another instance of Application.exe.
When "Blah" open Application.exe, I want this instance to know that the first instance is used by "Administrator". If Blah opens Application.exe twice, I want the second instance to end right away (I already do that), but I don't want to end Blah's instance if the first one is used by another user.
Is it clear ? (I'm not that good in English, sorry)
This shows a VBScript to do that, but you can do the same thing with WMI from within your VB program.
http://www.microsoft.com/technet/scriptcenter/guide/sas_prc_fywf.mspx
http://www.microsoft.com/technet/scriptcenter/guide/sas_prc_fywf.mspx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot for your help.
newood put me on the track and I finally went this way :
Dim query As New SelectQuery("Win32_Process ")
' Initialize an object searcher with this query
Dim searcher As New ManagementObjectSearcher(q uery)
' Get the resulting collection and loop through it
Dim mo As ManagementObject
Dim NbThisUser As Integer = 0
For Each mo In searcher.Get()
Dim arOwner(2) As String
If CStr(mo("Name")).ToLower = Diagnostics.Process.GetCur rentProces s.ProcessN ame.ToLowe r & ".exe" Then
mo.InvokeMethod("GetOwner" , arOwner)
If SystemInformation.UserName .ToLower = arOwner(0).ToLower And SystemInformation.UserDoma inName.ToL ower = arOwner(1).ToLower Then
NbThisUser += 1
If NbThisUser > 1 Then
End
End If
End If
End If
Next
newood put me on the track and I finally went this way :
Dim query As New SelectQuery("Win32_Process
' Initialize an object searcher with this query
Dim searcher As New ManagementObjectSearcher(q
' Get the resulting collection and loop through it
Dim mo As ManagementObject
Dim NbThisUser As Integer = 0
For Each mo In searcher.Get()
Dim arOwner(2) As String
If CStr(mo("Name")).ToLower = Diagnostics.Process.GetCur
mo.InvokeMethod("GetOwner"
If SystemInformation.UserName
NbThisUser += 1
If NbThisUser > 1 Then
End
End If
End If
End If
Next