private void GetSessionsBG() { try { RDP.TerminalSessions tS = new RDP.TerminalSessions(); Security.Impersonator sU = new Security.Impersonator(); RDP.Sessions tsSessions = new RDP.Sessions(); sU.StartImpersonation(tDomain, tUserName, tPassword); try { //Trace.WriteLine("Opening connection to server: " & tServerName) if (tS.OpenConnection(tServerName) == true) { tServerHandle = tS.ServerHandle; //Trace.WriteLine("Trying to get sessions") tsSessions = tS.GetSessions(); } } catch (Exception) { } int i = 0; //Trace.WriteLine("Sessions Count: " & tsSessions.Count) if (tServerName == this._CurrentHost) { for (i = 0; i <= tsSessions.ItemsCount - 1; i++) { ListViewItem lItem = new ListViewItem(); lItem.Tag = tsSessions[i].SessionID; lItem.Text = (string)(tsSessions[i].SessionUser); lItem.SubItems.Add(tsSessions[i].SessionState); lItem.SubItems.Add(Strings.Replace((string)(tsSessions[i].SessionName), Constants.vbNewLine, "", 1, -1, 0)); //Trace.WriteLine("Session " & i & ": " & tsSessions(i).SessionUser) AddToList(lItem); } } sU.StopImpersonation(); sU = null; tS.CloseConnection(tServerHandle); } catch (Exception ex) { Runtime.MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, Language.strSessionGetFailed + Constants.vbNewLine + ex.Message, true); } }
private void KillSessionBG() { try { if (tUserName == "" || tPassword == "") { //Trace.WriteLine("No Logon Info") return; } RDP.TerminalSessions ts = new RDP.TerminalSessions(); Security.Impersonator sU = new Security.Impersonator(); sU.StartImpersonation(tDomain, tUserName, tPassword); try { if (ts.OpenConnection(tServerName) == true) { if (ts.KillSession(tSessionID) == true) { sU.StopImpersonation(); //Trace.WriteLine("Successfully killed session: " & tSessionID) } else { sU.StopImpersonation(); //Trace.WriteLine("Killing session " & tSessionID & " failed") } } } catch (Exception) { sU.StopImpersonation(); } sU.StopImpersonation(); ClearList(); GetSessionsBG(); } catch (Exception ex) { Runtime.MessageCollector.AddMessage(Messages.MessageClass.ErrorMsg, Language.strSessionKillFailed + Constants.vbNewLine + ex.Message, true); } }