title: How to know if my application is controlled using Remote Desktop?
body: <p>Description of event <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.win32.systemevents.sessionswitch?view=dotnet-plat-ext-3.1" rel="nofollow noreferrer">SystemmEvents.SessionSwitch</a></p>
  <blockquote>
  <p>Occurs when the currently logged-in user has changed.</p>
  </blockquote>
  <p>The <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.win32.sessionswitcheventargs?view=dotnet-plat-ext-3.1" rel="nofollow noreferrer">SessionSwitchEventArgs</a> have a property <a href="https://docs.microsoft.com/en-us/dotnet/api/microsoft.win32.sessionswitcheventargs.reason?view=dotnet-plat-ext-3.1#Microsoft_Win32_SessionSwitchEventArgs_Reason" rel="nofollow noreferrer">Reason</a> which tells you the reason of the event.</p>
  <p>If remote desktop connects or disconnects, this event is raised.</p>
  <ul>
  <li>When starting remote desktop: Reason == SessionSwitchReason.RemoteConnect</li>
  <li>When remote desktop is closed: Reason == SessionSwitchReason.RemoteDisconnect</li>
  </ul>
  <p>.</p>
  <pre><code>class ViewModel
  {
      public ViewModel()
      {
          SystemEvents.SessionSwitch += SessionSwitched;
          IsRemoteSession = SystemParameters.IsRemoteSession;
  
          Logger.LogTrace(&quot;ViewModel constructed. Remote Session {0}&quot;, this.IsRemoteSession);
      }
  
      public bool IsRemoteSession {get; set;}
  
      private void SessionSwitched(object sender, SessionSwitchEventArgs e)
      {
          this.IsRemoteSession = SystemParameters.IsRemoteSession;
          Logger.LogTrace(&quot;Session Switch. Reason: {0}; Remote Session {1}&quot;
                           e.Reason, this.IsRemoteSession);
  }
  </code></pre>
  <p>If I start the program without remote desktop, property IsRemoteSession is false. If I start remote desktop, the event is raised, but the property remains false.</p>
  <p>However, if I start the program with remote descktop, IsRemoteSession is true. If I close remote desktop the event is raised, but the property remains true.</p>
  <p>I also tried GetSystemMetrics, as suggested <a href="https://stackoverflow.com/questions/159910/determine-if-a-program-is-running-on-a-remote-desktop">here</a>, but that didn't give the correct value either, which could be expected, because SystemParameters uses GetSystemMetrics to get the values.</p>