0

編集

プロセスに関連付けられているセッションIDのユーザー名をまだ割り当てることができません。

これは、ユーザーの詳細を取得するために使用するコードです。

    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

私のユーザークラスは単純に次のように定義されています。

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

プロセスクラスに関数を作成しました。

Public Sub AddUserInfo(ByVal UserName As String)
    _UserName = UserName
End Sub
4

1 に答える 1

1

これにより、同じ ID を持つプロセスがディクショナリに見つかった場合はプロセスが置き換えられ、そうでない場合は新しいプロセスが自動的に追加されます。

dictProcess(process.ProcessId) = process

編集(編集した質問への回答):

これはコレクションではなく、1 つのプロセスを表すことになっているため、Processesクラスの名前を に変更します。クラスProcessのコンストラクターを次のように変更できますProcess

Public Sub New(ByVal ProcessId As Integer, ByVal ProcessName As String, ByVal SessionId As Integer)            
    _ProcessId = ProcessId            
    _ProcessName = ProcessName            
    _SessionId = SessionId            
End Sub

次に、メソッドを追加します

Public Sub AddWmiInfo (ByVal PageFileUsage As Integer, ByVal WorkingSetSize As Integer)
    _PageFileUsage = PageFileUsage            
    _WorkingSetSize = WorkingSetSize            
End Sub

または、これらのプロパティを読み取り/書き込みにすることもできますが、このようにカプセル化をより適切に行うことができます。

Cassia を使用して基本的なプロセス情報をディクショナリに追加します。ProcessIdは as として宣言されIntegerているため、チェックNot String.IsNullOrEmpty(process.ProcessId)は意味をなさないことに注意してください。

For Each process As ITerminalServicesProcess In server.GetProcesses()                       
    dictprocess.Add( _
      process.ProcessId, _
      New Process(process.ProcessId, process.ProcessName, process.SessionId))                       
Next

最後に、次のように WMI からの情報を追加します。

Dim p As Process = Nothing
For Each wmiProcess In prowmi
    If dictprocess.TryGetValue(wmiProcess.ProcessID, p) Then
        p.AddWmiInfo(wmiProcess.PageFileUsage, wmiProcess.WorkingSetSize)
    End If
Next

TryGetValue変数にプロセスを返しますp。の 2 番目のパラメーターTryGetValueByRefパラメーターです。


編集#2:

あなたのループは読むべきです

Dim p As Process = Nothing
For Each user As User In dictuser.Values
    If dictprocess.TryGetValue(user.SessionID, p) Then
        p.AddUserInfo(user.UserName)
    End If
Next

ただし、キー (セッション ID) を使用してユーザー ディクショナリにアクセスすることがない場合は、ディクショナリよりもリストの方が適しています。

于 2012-01-20T21:49:04.037 に答える