Detecting remote desktop connection

前端 未结 9 1637
北恋
北恋 2020-11-30 19:51

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

相关标签:
9条回答
  • 2020-11-30 20:38

    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.

    isRDP = [System.Environment]
        .GetEnvironmentVariable("SESSIONNAME").StartsWith("RDP-")
    
    0 讨论(0)
  • 2020-11-30 20:38

    Well, I had a similar issue a few days ago. What I did to resolve it was took advantage of the fact that some Remote Desktop Application use a known default port, at least VNC and/or Microsoft Remote Desktop Connection. So I created a method which tells if the port is being used, as follows:

    /* Libraries needed */
    using System.Linq;
    using System.Net.NetworkInformation;
    
    /*....
      ....
      ....*/
    
    private static bool IsPortBeingUsed(int port)
    {
        return IPGlobalProperties.GetIPGlobalProperties().
                    GetActiveTcpConnections().
                        Any(
                                tcpConnectionInformation => 
                                tcpConnectionInformation.LocalEndPoint.Port == port
                           );
    }
    

    Remember to put the using statements with the libraries at the beginning of the file where the method is.

    You just have to pass for example a parameter like the 3389 port which is the default port for Remote Desktop Connection, or the 5900 port which is the default port for VNC Connections.

    The method is created with C# 4.0 features but it can perfectly be done with and older version of C#.Net or Visual Basic.

    This worked for me since I only needed to check for the two application I mentioned before.

    I hope it can help.

    0 讨论(0)
  • 2020-11-30 20:45

    If you don't want to add a reference to System.Windows.Forms.dll just for this (as suggested above), then you can also call the underlying system call directly via PInvoke, like this:

        int result = GetSystemMetrics(SystemMetric.SM_REMOTESESSION);
        bool isRemoteSession = (result != 0);
    

    The SystemMetric enumeration can be found at PInvoke.net - SystemMetric (but you can just use the value of 0x1000); while the signature for GetSystemMetrics at PInvoke.net - GetSystemMetrics.

    I tested this with RDP and VNC - works with the former (admin/console mode also), does not detect the latter.

    0 讨论(0)
提交回复
热议问题