Advertisement
magnusbakken

Untitled

Nov 16th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. using System.Diagnostics;
  2. using System.Linq;
  3. using System.Runtime.InteropServices;
  4.  
  5. namespace DetectRemoteApp
  6. {
  7.     public static class RemoteDesktopDetection
  8.     {
  9.         private const int RemoteSessionSystemMetricId = 0x1000;
  10.  
  11.         [DllImport("user32.dll")]
  12.         private static extern int GetSystemMetrics(int nIndex);
  13.  
  14.         public static DesktopMode DetectDesktopMode()
  15.         {
  16.             if (IsLocalSession())
  17.                 return DesktopMode.Local;
  18.             else if (IsRemoteAppSession())
  19.                 return DesktopMode.RemoteApp;
  20.             else
  21.                 return DesktopMode.RemoteDesktop;
  22.         }
  23.  
  24.         private static bool IsLocalSession()
  25.         {
  26.             return GetSystemMetrics(RemoteSessionSystemMetricId) == 0;
  27.         }
  28.  
  29.         private static bool IsRemoteAppSession()
  30.         {
  31.             // This test isn't perfect, since we don't know for sure if rdpinit.exe will always be running,
  32.             // and there could theoretically be an unrelated process with the same name.
  33.             // Normally we'd be able to test if rdpinit.exe is a parent of our process,
  34.             // but that won't work for ClickOnce apps because they're children of dfsvc.exe.
  35.             int currentSessionId = Process.GetCurrentProcess().SessionId;
  36.             return Process.GetProcesses().Where(p => p.SessionId == currentSessionId)
  37.                 .Any(p => p.ProcessName == "rdpinit");
  38.         }
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement