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

.net 如何判断我的应用程序是否在RDP会话中运行,.net,winforms,rdp,.net,Winforms,Rdp,我有一个.net winforms应用程序,它有一些动画效果、淡入和滚动动画等。这些都可以很好地工作,但是如果我在远程桌面协议会话中,动画就会开始变糟 有人能建议一种方法来确定应用程序是否在RDP会话中运行,这样我就可以在这种情况下关闭效果了吗?使用user32.dll中的函数。过去常叫它。下面是第一个链接提供的示例代码。第二个链接告诉您如何在.NET中调用它 BOOL IsRemoteSession(void){ return GetSystemMetrics( SM_REMOT

我有一个.net winforms应用程序,它有一些动画效果、淡入和滚动动画等。这些都可以很好地工作,但是如果我在远程桌面协议会话中,动画就会开始变糟

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

使用user32.dll中的函数。过去常叫它。下面是第一个链接提供的示例代码。第二个链接告诉您如何在.NET中调用它

 BOOL IsRemoteSession(void){
      return GetSystemMetrics( SM_REMOTESESSION );
   }
完整代码:

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

还有
SystemInformation.TerminalServerSession
属性,用于确定客户端是否连接到终端服务器会话。by MSDN非常广泛,因此我不会在这里重复它。

假设您至少在.NET Framework 2.0上,就不需要使用p/Invoke:只需检查
System.Windows.Forms.SystemInformation.TerminalServerSession
()的值

除了进行初始检查以查看您的桌面是否在RDP会话中运行外,您还可能需要处理ap运行时远程会话连接或断开的情况。您可以在控制台会话上运行一个应用程序,然后有人可以启动到控制台的RDP连接。除非您的应用程序定期调用GetSystemMetrics,否则它将假定它不是作为终端服务会话运行

您将通过WTSRegisterSessionNotification让应用程序注册会话更新通知。这将允许应用程序在应用程序运行的桌面会话的远程连接已打开或关闭时立即收到通知。有关C#代码示例,请参见


有关使用WTSRegisterSessionNotification的一些好的Delphi Win32示例代码,请参见此。

请参见我提出的类似问题:

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

/// <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;    
    }
}


注意:这个问题已经被问过了()

注意
TerminalServerSession
只是对
GetSystemMetrics(SM\u REMOTESESSION)
调用的包装。
/// <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;
      }
   }
}
public Boolean UseAnimations()
{
   return 
      (!System.Windows.Forms.SystemInformation.TerminalServerSession) &&
      (System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus != PowerLineStatus.Offline);
}