1

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 ?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
user609511
  • 4,091
  • 12
  • 54
  • 86
  • take a look here: http://stackoverflow.com/a/1768219/1300049 – JleruOHeP Dec 28 '12 at 10:09
  • Is this what you're refering to? http://stackoverflow.com/questions/804700/how-to-find-fqdn-of-local-machine-in-c-net – eestein Dec 28 '12 at 10:10
  • 3
    Are you asking how to get the TERMINAL's name while running your application inside your SERVER? What terminal technology are you using? This isn't what client-server means by the way – Panagiotis Kanavos Dec 28 '12 at 10:13
  • YES, terminal name. this terminal is a light PC, they don't have a CD driver nor hardisk. it just a small box that connect to server TSE. They even not have an Operating System installed – user609511 Dec 28 '12 at 10:15

4 Answers4

8

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.

Christoph
  • 3,322
  • 2
  • 19
  • 28
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 1
    @Jerther in some cases `CLIENTNAME` returns `Console` as described in this [Microsoft KB article](https://support.microsoft.com/en-us/kb/971165). I don't remember what else I had found 4 years ago. By now of course Windows XP is no longer supported – Panagiotis Kanavos May 25 '16 at 13:58
  • @Jerther also found [this KB article about RDP connections](https://support.microsoft.com/en-us/kb/2509192) which applies to Windows 7 and 2008 R2. – Panagiotis Kanavos May 25 '16 at 14:05
3

On the client, use:

string machineName = Environment.MachineName;
Dave Bish
  • 19,263
  • 7
  • 46
  • 63
0
string ComputerName1 = Dns.GetHostName();//Server Name
string ComputerName2 = Environment.MachineName;//Server Name  
Rohit Vyas
  • 1,949
  • 3
  • 19
  • 28
-1

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 :)

phwt
  • 1,356
  • 1
  • 22
  • 42