我的经理向我指出了我们的一个会计应用程序可以做的一些漂亮的事情,因为当在 RDP 会话中打开包时,它可以根据主机的机器名称和客户端的机器名称加载不同的设置。
我们希望在我公司的一个应用程序中提供类似的功能。
我在这个站点上发现了如何检测我是否在 RDP 会话中,但是我无法在任何地方找到有关如何检测客户端计算机名称的信息。
任何朝着正确方向的指针都会很棒。
我正在为 .NET 3.5 编写 C#
编辑
我从下面的建议中拼凑的示例代码 - 对于任何使用 WTSQuerySessionInformation 的人来说应该足以了解正在发生的事情。请注意,这不一定是最好的方法 - 只是我发现有用的一个起点。
当我在本地运行它时,我会得到无聊的预期答案。当我在 RDP 会话中的本地办公室服务器上运行它时,我在 WTSClientName 属性中看到了我自己的计算机名称。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace TerminalServicesTest
{
class Program
{
const int WTS_CURRENT_SESSION = -1;
static readonly IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
uint byteCount;
foreach (WTS_INFO_CLASS item in Enum.GetValues(typeof(WTS_INFO_CLASS)))
{
Program.WTSQuerySessionInformation(
WTS_CURRENT_SERVER_HANDLE,
WTS_CURRENT_SESSION,
item,
out sb,
out byteCount);
Console.WriteLine("{0}({1}): {2}", item.ToString(), byteCount, sb);
}
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
[DllImport("Wtsapi32.dll")]
public static extern bool WTSQuerySessionInformation(
IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out StringBuilder ppBuffer, out uint pBytesReturned);
}
enum WTS_INFO_CLASS
{
WTSInitialProgram = 0,
WTSApplicationName = 1,
WTSWorkingDirectory = 2,
WTSOEMId = 3,
WTSSessionId = 4,
WTSUserName = 5,
WTSWinStationName = 6,
WTSDomainName = 7,
WTSConnectState = 8,
WTSClientBuildNumber = 9,
WTSClientName = 10,
WTSClientDirectory = 11,
WTSClientProductId = 12,
WTSClientHardwareId = 13,
WTSClientAddress = 14,
WTSClientDisplay = 15,
WTSClientProtocolType = 16,
WTSIdleTime = 17,
WTSLogonTime = 18,
WTSIncomingBytes = 19,
WTSOutgoingBytes = 20,
WTSIncomingFrames = 21,
WTSOutgoingFrames = 22,
WTSClientInfo = 23,
WTSSessionInfo = 24,
WTSSessionInfoEx = 25,
WTSConfigInfo = 26,
WTSValidationInfo = 27,
WTSSessionAddressV4 = 28,
WTSIsRemoteSession = 29
}
}