Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Diagnostics;
- using System.Linq;
- using System.Runtime.InteropServices;
- namespace DetectRemoteApp
- {
- public static class RemoteDesktopDetection
- {
- private const int RemoteSessionSystemMetricId = 0x1000;
- [DllImport("user32.dll")]
- private static extern int GetSystemMetrics(int nIndex);
- public static DesktopMode DetectDesktopMode()
- {
- if (IsLocalSession())
- return DesktopMode.Local;
- else if (IsRemoteAppSession())
- return DesktopMode.RemoteApp;
- else
- return DesktopMode.RemoteDesktop;
- }
- private static bool IsLocalSession()
- {
- return GetSystemMetrics(RemoteSessionSystemMetricId) == 0;
- }
- private static bool IsRemoteAppSession()
- {
- // This test isn't perfect, since we don't know for sure if rdpinit.exe will always be running,
- // and there could theoretically be an unrelated process with the same name.
- // Normally we'd be able to test if rdpinit.exe is a parent of our process,
- // but that won't work for ClickOnce apps because they're children of dfsvc.exe.
- int currentSessionId = Process.GetCurrentProcess().SessionId;
- return Process.GetProcesses().Where(p => p.SessionId == currentSessionId)
- .Any(p => p.ProcessName == "rdpinit");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement