[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Is there a better way to get user status on a terminal server in C#?

Posted on 2009-07-02
1
Medium Priority
?
551 Views
Last Modified: 2012-05-07
I need to get user status for a specific user on a terminal server in c#.

I can do this by calling query.exe via System.Diagnostics.Process.
And then read/split the last line of the output stream.
As i do in the code example.

I need to use this in a web service, which is running on a different server, and i don't think this is the proper way to do this.

So is there a better way to do this.

public class TSTools
{
    public static TSInfo GetTSInfo(string server, string user)
    {
        Process process = new Process();
 
        process.StartInfo.FileName = "query.exe";
        process.StartInfo.Arguments = "user " + user + " /server:" + server;
        process.StartInfo.UseShellExecute = false;
        process.StartInfo.RedirectStandardOutput = true;
        process.Start();
 
        string lastLine = null;
        while (!process.StandardOutput.EndOfStream)
        {
            lastLine = process.StandardOutput.ReadLine();
        }
 
        TSInfo tsInfo = new TSInfo();
        if (!string.IsNullOrEmpty(lastLine))
        {
            tsInfo.UserName = lastLine.Substring(0, 22).Trim();
            tsInfo.SessionName = lastLine.Substring(22, 17).Trim();
            tsInfo.SessionID = lastLine.Substring(39, 6).Trim();
            tsInfo.Status = lastLine.Substring(45, 9).Trim();
            tsInfo.IdleTime = lastLine.Substring(54, 10).Trim();
            tsInfo.LogonTime = lastLine.Substring(64).Trim();
        }
        else
        {
            tsInfo.UserName = user;
            tsInfo.SessionName = "";
            tsInfo.SessionID = "";
            tsInfo.Status = "Offline";
            tsInfo.IdleTime = "";
            tsInfo.LogonTime = "";
        }
 
        return tsInfo;
    }
}
 
public class TSInfo
{
    public string UserName { get; set; }
    public string SessionName { get; set; }
    public string SessionID { get; set; }
    public string Status { get; set; }
    public string IdleTime { get; set; }
    public string LogonTime { get; set; }
}

Open in new window

0
Comment
Question by:thomas_eg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 18

Accepted Solution

by:
Gary Davis earned 1500 total points
ID: 24765102
This is the "Unix" way of doing things. Many Unix commands are written to have their output easily parsed and used by other programs.
I think this idea is fine. Windows is not as fast as Unix in creating processes but if your process does not call the web service too quickly, perfomance should be OK.
The advantage is that the Query.exe is already written and debugged. You could write your parse routine to be a little more flexible so blank delimiters are used in stead of the actual column locations.
Gary Davis
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question