如何判断我的应用程序是否在 RDP 会话中运行
我有一个 .net winforms 应用程序,它有一些动画效果、淡入和滚动动画等。这些效果很好,但是如果我处于远程桌面协议会话中,动画就会开始变得刺耳。
有人可以建议一种方法来确定应用程序是否正在通过 RDP 会话运行,以便我可以在这种情况下关闭效果吗?
I have a .net winforms app which has a few animation effects, fade ins and scroll animations etc. These work fine however if I'm in a Remote Desktop Protocol session the animations start to grate.
Can someone suggest a way of determining whether or not an app is running across an RDP session so I can turn the effects off in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您至少使用 .NET Framework 2.0,则无需使用 P/Invoke:只需检查
System.Windows.Forms.SystemInformation.TerminalServerSession
(MSDN)。Assuming you're at least on .NET Framework 2.0, there's no need to use P/Invoke: just check the value of
System.Windows.Forms.SystemInformation.TerminalServerSession
(MSDN).请参阅我提出的类似问题:如何检查我们是否'正在使用电池运行?
因为如果您使用电池运行,您还想禁用动画。
然后检查您是否使用电池运行:
您可以将其合并为一个:
注意:这个问题已经被问过(确定程序是否正在远程桌面上运行)
See a similar question i asked: How to check if we’re running on battery?
Because if you're running on battery you also want to disable animations.
And then to check if you're running on battery:
Which you can just combine into one:
Note: This question was already asked (Determine if a program is running on a Remote Desktop)
除了进行初始检查以查看您的桌面是否在 RDP 会话中运行之外,您可能还需要处理应用程序运行时远程会话连接或断开连接的情况。 您可以在控制台会话上运行一个应用程序,然后有人可以启动与控制台的 RDP 连接。 除非您的应用程序定期调用 GetSystemMetrics,否则它将假定它不作为终端服务会话运行。
您可以让您的应用程序通过 WTSRegisterSessionNotification 注册会话更新通知。 如果您的应用程序正在运行的桌面会话的远程连接已打开或关闭,那么您的应用程序将立即收到通知。 有关一些示例 C#,请参阅此处代码。
有关使用 WTSRegisterSessionNotification 的一些优秀的 Delphi Win32 示例代码,请参阅此页面。
In addition to making the initial check to see if your desktop is running in a RDP session, you may also want to handle the situation where the remote session is connected or disconnected while your ap is running. You could have an app running on the console session and then someone could initiate a RDP connection to the console. Unless your application is periodically making the call to GetSystemMetrics, it's going to assume that it's not running as a terminal services session.
You would have your app register for session update notificiations through WTSRegisterSessionNotification. That will allow your applicaiton to be immediately notified is a remote connection has been opened or closed to the desktop session that your application is running under. See here for some sample C# code.
For a some good Delphi Win32 exampale code for using WTSRegisterSessionNotification, see this page.
使用 user32.dll 中的 GetSystemMetrics() 函数。 使用 PInvoke 调用它。 以下是第一个链接提供的示例代码。 第二个链接告诉您如何在 .NET 中调用它。
完整代码:
还有
SystemInformation.TerminalServerSession
属性,它确定客户端是否连接到终端服务器会话。 提供的代码 内容很广泛,所以我不会在这里重复它。Use the GetSystemMetrics() function in the user32.dll. Use PInvoke to call it. The following is sample code provided by the first link. The second link tells you how to invoke it in .NET.
Complete code:
There's also the
SystemInformation.TerminalServerSession
Property, which determines whether or not the client is connected to a Terminal Server session. The code provided by MSDN is extensive, so I won't duplicate it here.