如何判断我的应用程序是否在RDP会话中运行

Chr*_*rds 18 .net rdp winforms

我有一个.net winforms应用程序,它有一些动画效果,淡入和滚动动画等.这些工作正常但是如果我在远程桌面协议会话中动画开始grate.

有人可以建议一种方法来确定应用程序是否在RDP会话中运行,这样我可以在这种情况下关闭效果吗?

Arn*_*out 20

假设您至少使用的是.NET Framework 2.0,则无需使用P/Invoke:只需检查System.Windows.Forms.SystemInformation.TerminalServerSession(MSDN)的值即可.


Ian*_*oyd 7

看到我问的类似问题:如何检查我们是否使用电池运行?

因为如果您使用电池运行,您还需要禁用动画.

/// <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;    
    }
}
Run Code Online (Sandbox Code Playgroud)

然后检查你是否使用电池运行:

/// <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;
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

您可以将其组合成一个:

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

注意:此问题已被提出(确定程序是否在远程桌面上运行)