【问题标题】:WPF identify remote access (Not Winforms)WPF 识别远程访问(不是 Winforms)
【发布时间】:2011-12-01 10:03:23
【问题描述】:

WPF 应用程序如何知道它是否被远程操作(通过 VNC 或远程桌面)?

在 winforms 中有 System.Windows.Forms.SystemInformation.TerminalServerSession 根据Detecting remote desktop connection,但在 WPF 中是否有直接的方法?

我想现在的破解方法可能是在 WPF 上拥有一个不可见的 Winforms 主机,并使用它自己的能力来托管可以识别相同内容的虚拟 win 表单......但这对我来说看起来很蹩脚!

任何意见将不胜感激!

谢谢

【问题讨论】:

    标签: wpf remote-desktop remote-access


    【解决方案1】:

    我想现在的破解方法可能是在 WPF 上拥有一个不可见的 Winforms 主机,并使用它自己的能力来托管可以识别相同内容的虚拟 win 表单......但这对我来说看起来很蹩脚!

    您不需要不可见的 WinForms 主机...您只需添加对 System.Windows.Forms 程序集的引用,并使用 SystemInformation.TerminalServerSession 静态属性。

    如果您不想依赖 WinForms,可以使用GetSystemMetrics Win32 API:

    const int SM_REMOTESESSION = 0x1000;
    
    [DllImport("user32")]
    static extern int GetSystemMetrics(int nIndex);
    
    
    public static bool IsTerminalServerSession()
    {
        return (GetSystemMetrics(SM_REMOTESESSION) & 1) != 0;
    }
    

    【讨论】:

    • +1 哦,我与看不到这种差异的人打过仗! GRRRR!!!
    【解决方案2】:

    此方法不需要 Windows 窗体窗口,只需要对 DLL 的引用。

    如果不想引用这个,可以自己调用方法检查这个,实现如下(我已经封装在一个类中了):

    static class SystemInformation
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        private static extern int GetSystemMetrics(int nIndex);
        public static bool IsTerminalServerSession
        {
            get
            {
                //copied the Windows Forms implementation
                return (GetSystemMetrics(0x1000) & 1) != 0;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 1970-01-01
      • 2019-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多