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 t
For WPF applications there is
System.Windows.SystemParameters.IsRemoteSession
and System.Windows.SystemParameters.IsRemotelyControlled
.
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.StandardOutput.ReadLine();
while (true)
{
string strLine = proc.StandardOutput.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, ...)
}
Just a note to say that using GetSystemMetrics(SystemMetric.SM_REMOTESESSION) on its own has stopped being reliable for Windows 8 / Server 2012 onwards if the session is using RemoteFX virtualisation of the GPU.
The "official" method of detecting RDS is described by Microsoft here: Detecting the Remote Desktop Services environment (Last updated 31 May 18).
It consists of using the SystemMetrics call and a registry check at
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Terminal Server\GlassSessionId
The code samples in that article are C++ only, but given that it's just a registry lookup, I don't think folks will find it too tricksty to replicate in other languages.
I'd like to hope that at least some of the .Net built in functions mentioned upthread are following this in full, but :
SystemParameters.IsRemoteSession is noted here as "Maps to SM_REMOTESESSION. See GetSystemMetrics", and
SystemParameters.IsRemotelyControlled is noted here as the same,
so I'm not optimistic.
I'll try to do some detailed checks shortly and post the results.
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 ;-)
All remote login programs require a service or program running on the local machine. The questioner only needs to worry about VNC and its clones if those services or programs are allowed to run on his local machine. They are not necessary for Remote Desktop use, and there are Remote Desktop clients for all operating systems. A VNC server is unnecessary if Remote Desktop is working.
Furthermore, the VNC clones can't log in for you unless you install them as an administrator on the server machine. As long as you don't let users run processes as other users, the only concern is if one of your other employees is logging in as the problematic one. And if that's the case, no technical solution is going to be sufficient. Even if you have individual cards for each employee that must be used to log in, the problematic employee could just give his friend the card.
For Windows Store apps, you can use this:
Windows.System.RemoteDesktop.InteractiveSession.IsRemote