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

Posted on 2009-07-02
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;


        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();




            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

Question by:thomas_eg
1 Comment
LVL 18

Accepted Solution

Gary Davis earned 500 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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data Saving(2) 2 41
Split string on commas but not when enclosed in parentheses 7 47
Icons and Colors for Terms 3 24
Syntax Error 2 42
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now