How can i get the Client Computer name

2020-07-28 11:20发布

I am using C# Framework 4.0 Windows Form. My program is installed on a server TSE. There are 11 light clients that connect to this server.

When one of these clients launches my progam, how can I get his PC name ?

4条回答
乱世女痞
2楼-- · 2020-07-28 11:41

Assuming you are using Terminal Services and Remote Desktop, you can check the CLIENTNAME environment variable to retrieve the client machine's name, although some people report problems with it.

You can get the value with Environment.GetEnvironmentVariable, eg

var clientName=Environment.GetEnvironmentVariable("CLIENTNAME");

For an API based method, check Preferred way Of getting Client name From Terminal Server Session which shows how to use WMI or the Terminal Services API to retrieve the client name

UPDATE

Hmm, seems the Cassia library encapsulates the TS API. You can get the client name with

new TerminalServicesManager().CurrentSession.ClientName

Another SO thread shows how to retrieve the current session's client name or the client names of all sessions.

查看更多
混吃等死
3楼-- · 2020-07-28 11:41
string ComputerName1 = Dns.GetHostName();//Server Name
string ComputerName2 = Environment.MachineName;//Server Name  
查看更多
Juvenile、少年°
4楼-- · 2020-07-28 11:41

If you are using .NET you can use the code below :

Dim PcName As String = Environment.UserName

Then just call the PcName at anywhere you want to display.

Goodluck :)

查看更多
我欲成王,谁敢阻挡
5楼-- · 2020-07-28 11:42

On the client, use:

string machineName = Environment.MachineName;
查看更多
登录 后发表回答