How do I tell if my application is running in an RDP session

你。 提交于 2019-11-30 06:45:49

Assuming you're at least on .NET Framework 2.0, there's no need to use P/Invoke: just check the value of System.Windows.Forms.SystemInformation.TerminalServerSession (MSDN).

Ian Boyd

See a similar question i asked: How to check if we’re running on battery?

Because if you're running on battery you also want to disable animations.

/// <summary>
/// Indicates if we're running in a remote desktop session.
/// If we are, then you MUST disable animations and double buffering i.e. Pay your taxes!
/// 
/// </summary>
/// <returns></returns>
public static Boolean IsRemoteSession
{
    //This is just a friendly wrapper around the built-in way
    get    
    {
        return System.Windows.Forms.SystemInformation.TerminalServerSession;    
    }
}

And then to check if you're running on battery:

/// <summary>
/// Indicates if we're running on battery power.
/// If we are, then disable CPU wasting things like animations, background operations, network, I/O, etc
/// </summary>
public static Boolean IsRunningOnBattery
{
   get
   {
      PowerLineStatus pls = System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus;
      if (pls == PowerLineStatus.Offline)
      {
         //Offline means running on battery
         return true;
      }
      else
      {
         return false;
      }
   }
}

Which you can just combine into one:

public Boolean UseAnimations()
{
   return 
      (!System.Windows.Forms.SystemInformation.TerminalServerSession) &&
      (System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus != PowerLineStatus.Offline);
}

Note: This question was already asked (Determine if a program is running on a Remote Desktop)

In addition to making the initial check to see if your desktop is running in a RDP session, you may also want to handle the situation where the remote session is connected or disconnected while your ap is running. You could have an app running on the console session and then someone could initiate a RDP connection to the console. Unless your application is periodically making the call to GetSystemMetrics, it's going to assume that it's not running as a terminal services session.

You would have your app register for session update notificiations through WTSRegisterSessionNotification. That will allow your applicaiton to be immediately notified is a remote connection has been opened or closed to the desktop session that your application is running under. See here for some sample C# code.

For a some good Delphi Win32 exampale code for using WTSRegisterSessionNotification, see this page.

Use the GetSystemMetrics() function in the user32.dll. Use PInvoke to call it. The following is sample code provided by the first link. The second link tells you how to invoke it in .NET.

 BOOL IsRemoteSession(void){
      return GetSystemMetrics( SM_REMOTESESSION );
   }

Complete code:

[DllImport("User32.dll")]
static extern Boolean IsRemoteSession()
{
 return GetSystemMetrics ( SM_REMOTESESSION);
}

There's also the SystemInformation.TerminalServerSession Property, which determines whether or not the client is connected to a Terminal Server session. The code provided by MSDN is extensive, so I won't duplicate it here.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!