EDIT
I still can't assign the process the username of the session id that is associated with it.
This is the code i use to retrieve user details:
Public Sub GetUsers()
Using server As ITerminalServer = manager.GetRemoteServer(strHostName)
server.Open()
For Each session As ITerminalServicesSession In server.GetSessions()
If Not String.IsNullOrEmpty(session.UserName) Then
dictuser.Add(session.SessionId, New User(session.SessionId, session.UserName))
End If
Next
End Using
End Sub
my user class is defined simply as:
Public Class User
Private _SessionID As Integer
Private _UserName As String
Sub New(ByVal SessionID As Integer, ByVal UserName As String)
_SessionID = SessionID
_UserName = UserName
End Sub
Public ReadOnly Property SessionID As String
Get
Return _SessionID
End Get
End Property
Public ReadOnly Property UserName As String
Get
Return _UserName
End Get
End Property
End Class
i have created a function in my process class:
Public Sub AddUserInfo(ByVal UserName As String)
_UserName = UserName
End Sub
This replaces the process if a process with the same id is found in the dictionary and automatically adds a new process otherwise.
EDIT (in response to your edited question):
I would change the name of the
Processes
class toProcess
, since it is not a collection, but is supposed to represent one process. You could change the constructor of theProcess
class toThen add a method
Alternatively, you could also make these properties read/write, however you get a better encapsulation like this.
Add the basic process information to the dictionary using Cassia. Note that
ProcessId
is declared asInteger
and thus the checkNot String.IsNullOrEmpty(process.ProcessId)
makes no sense.Finally, you would add the information from the WMI like this
TryGetValue
returns a process in the variablep
. The second parameter ofTryGetValue
is aByRef
parameter.EDIt #2:
Your loop should read
However, if you are never accessing the user dictionary through the key (session id), then a list would be more appropriate than a dictionary.