public TerminalSession[] GetTerminalServicesSessions() { try { Log.WriteStart("GetTerminalServicesSessions"); List <TerminalSession> sessions = new List <TerminalSession>(); string ret = FileUtils.ExecuteSystemCommand("qwinsta", ""); // parse returned string StringReader reader = new StringReader(ret); string line = null; int lineIndex = 0; while ((line = reader.ReadLine()) != null) { /*if (line.IndexOf("USERNAME") != -1 ) * continue;*/ // if (lineIndex == 0) { lineIndex++; continue; } Regex re = new Regex(@"(\S+)\s+", RegexOptions.Multiline | RegexOptions.IgnoreCase); MatchCollection matches = re.Matches(line); // add row to the table string username = ""; if (matches.Count > 4) { username = matches[1].Value.Trim(); } if (username != "") { TerminalSession session = new TerminalSession(); // session.SessionId = Int32.Parse(matches[2].Value.Trim()); session.Username = username; session.Status = matches[3].Value.Trim(); sessions.Add(session); } // lineIndex++; } reader.Close(); Log.WriteEnd("GetTerminalServicesSessions"); return(sessions.ToArray()); } catch (Exception ex) { Log.WriteError("GetTerminalServicesSessions", ex); throw; } }
public TerminalSession[] GetTerminalServicesSessions() { try { Log.WriteStart("GetTerminalServicesSessions"); List<TerminalSession> sessions = new List<TerminalSession>(); string ret = FileUtils.ExecuteSystemCommand("qwinsta", ""); // parse returned string StringReader reader = new StringReader(ret); string line = null; int lineIndex = 0; while ((line = reader.ReadLine()) != null) { /*if (line.IndexOf("USERNAME") != -1 ) continue;*/ // if (lineIndex == 0) { lineIndex++; continue; } Regex re = new Regex(@"(\S+)\s+", RegexOptions.Multiline | RegexOptions.IgnoreCase); MatchCollection matches = re.Matches(line); // add row to the table string username = ""; if (matches.Count > 4) username = matches[1].Value.Trim(); if (username != "") { TerminalSession session = new TerminalSession(); // session.SessionId = Int32.Parse(matches[2].Value.Trim()); session.Username = username; session.Status = matches[3].Value.Trim(); sessions.Add(session); } // lineIndex++; } reader.Close(); Log.WriteEnd("GetTerminalServicesSessions"); return sessions.ToArray(); } catch (Exception ex) { Log.WriteError("GetTerminalServicesSessions", ex); throw; } }