How can i get the Client Computer name

21,515

Solution 1

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.

Solution 2

On the client, use:

string machineName = Environment.MachineName;
Share:
21,515
user609511
Author by

user609511

Updated on July 09, 2022

Comments

  • user609511
    user609511 almost 2 years

    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 ?

    • JleruOHeP
      JleruOHeP over 11 years
    • eestein
      eestein over 11 years
      Is this what you're refering to? stackoverflow.com/questions/804700/…
    • Panagiotis Kanavos
      Panagiotis Kanavos over 11 years
      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
    • user609511
      user609511 over 11 years
      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
  • nflash
    nflash almost 9 years
    This does not answer the question as the question asks for the client name and not the server (RD Host)
  • Rohit Vyas
    Rohit Vyas almost 9 years
    Then What would be the answer? @nflash ??
  • Panagiotis Kanavos
    Panagiotis Kanavos almost 8 years
    @Jerther in some cases CLIENTNAME returns Console as described in this Microsoft KB article. I don't remember what else I had found 4 years ago. By now of course Windows XP is no longer supported
  • Panagiotis Kanavos
    Panagiotis Kanavos almost 8 years
    @Jerther also found this KB article about RDP connections which applies to Windows 7 and 2008 R2.
  • André Pletschette
    André Pletschette almost 2 years
    it's about the Citrix Clientname, not the username