tags:

views:

1511

answers:

3

Is there anyway, in a program, to detect if a program is being run from inside a remote desktop session or if the program is being run normal in .NET 2.0? What I'm trying to do is, I create a timeclock application which will clock a person in and out and keep track. But this particular person, I suspect, is remoting into their computer at work, from home, and clocking in and out.

Any ideas how I can solve this issue (and taking away remote desktop access is not an option)? My idea is, if there is a way to detect remote desktop sessions, I will simply implement this into the progam and prevent them from clocking in remotely.

+2  A: 

http://www.appdeploy.com/messageboards/tm.asp?m=21420&mpage=1&key=&#21420

The system variable %sessionname% will return Console if its local or RDP* if its remote.

System.Environment.ExpandEnvironmentVariables("sessionname")
John Gietzen
If you RDP with the /console switch (or /admin, depending on the version), then it returns "Console" (I've just checked ;-p)
Marc Gravell
+7  A: 

allegedly,

System.Windows.Forms.SystemInformation.TerminalServerSession

will be true for a remote desktop session (or VNC session)

but i'd test it to be sure ;-)

Steven A. Lowe
I highly doubt this will pick up VNC sessions. It will probably detect RDP.
Spencer Ruport
I've checked, and (unlike %sessionname% - other answer) this works fine even with the /console or /admin switch. This sounds like the best answer for MSTSC.
Marc Gravell
+2  A: 

If you're concerned about VNC, it looks like it would be possible to check open TCP connections with netstat. In a command prompt, type:

netstat -n -a -p tcp

and check to see if the port 5900 is "ESTABLISHED". Of course, 5900 is the default connection port, so it would be dependent on what port is set.

From there, I found this post at CodeGuru that explains how to use netstat in your c# program:

string sCommand = "netstat";
string sArgs = "";
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo (sCommand, sArgs);

psi.UseShellExecute = false;
psi.RedirectStandartOutput = true;

System.Diagnostics.Process proc = null;
proc = System.Diagnostics.Process.Start(psi);
proc.WaitForExit();

// Read the first 4 lines. They don't contain any information we need to get
for (int i = 0; i < 4; i++)
    proc.StandartOutput.ReadLine();

while (true)
{
    string strLine = proc.StandartOutput.ReadLine();
    if (strLine == null)
        break;

    // Analyze the line 
    // Line is in following structure:
    // Protocol (TCP/UDP)   Local Address(host:port) Foreign Address(host:port) State(ESTABLISHED, ...)
}
Jared Harley