C#确定远程桌面登录用户的计算机名 [英] C# Determine remote desktop login user's computer name
问题描述
我一直在研究了几个星期,现在,上下车,对如何确定是通过远程桌面登录的用户的计算机名称。
I have been researching for a couple of weeks now, on and off, on how to determine the computer name of the user that is logged in via remote desktop.
我有一个用户在终端服务器环境中运行的应用程序,我想捕获和存储他们使用的是连接到终端服务器的计算机的名称。
I have an application that users run on a terminal server environment, and I would like to capture and store the name of the computer that they are using to connect to the terminal server with.
到目前为止,我还没有能够找到code或创建自己能做到这一点,我想我只是没有提出正确的问题。
So far, I have not been able to find code or create my own that can do this, and I think I am just not asking the right questions.
任何援助将大大AP preciated。
Any assistance would be greatly appreciated.
PS。我使用C#和.NET 4.0
PS. I am using C# and .Net 4.0
推荐答案
好了,所以我找到了一个解决方案的http:// www.amasso.info/?p=165
Okay so I have found a solution at http://www.amasso.info/?p=165
code转载...
Code reproduced below...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Principal;
using System.Net;
namespace loginName
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
WindowsPrincipal wp = new WindowsPrincipal(WindowsIdentity.GetCurrent());
MessageBox.Show(wp.Identity.Name); // Username
MessageBox.Show(GetTerminalServerClientNameWTSAPI()); // Remote Host PC Name
}
private static string GetTerminalServerClientNameWTSAPI()
{
const int WTS_CURRENT_SERVER_HANDLE = -1;
IntPtr buffer = IntPtr.Zero;
uint bytesReturned;
string strReturnValue = "";
try
{
WTSQuerySessionInformation(IntPtr.Zero, WTS_CURRENT_SERVER_HANDLE, WTS_INFO_CLASS.WTSClientName, out buffer, out bytesReturned);
strReturnValue = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(buffer);
}
finally
{
buffer = IntPtr.Zero;
}
return strReturnValue;
}
enum WTS_INFO_CLASS
{
WTSInitialProgram,
WTSApplicationName,
WTSWorkingDirectory,
WTSOEMId,
WTSSessionId,
WTSUserName,
WTSWinStationName,
WTSDomainName,
WTSConnectState,
WTSClientBuildNumber,
WTSClientName,
WTSClientDirectory,
WTSClientProductId,
WTSClientHardwareId,
WTSClientAddress,
WTSClientDisplay,
WTSClientProtocolType
}
[System.Runtime.InteropServices.DllImport("Wtsapi32.dll")]
private static extern bool WTSQuerySessionInformation(System.IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);
}
}
这篇关于C#确定远程桌面登录用户的计算机名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!