views:

134

answers:

4

I need to get the host name currently running the application. Any idea?

+6  A: 

Unless I am mistaken on what you want to do..

System.Environment.MachineName
Wil
A: 
System.Windows.Forms.SystemInformation.ComputerName;
rmx
+6  A: 

Something to bear in mind is that System.Environment.MachineName; and System.Windows.Forms.SystemInformation.ComputerName; will give you the NETBIOS name of the machine (restricted to 15 characters).

If you want the full TCP/IP based host name you can use Dns.GetHostName():

string hostName = System.Net.Dns.GetHostName();

Or you can use:

System.Environment.GetEnvironmentVariable("COMPUTERNAME");

Which will return the full computer name set during installation.

GenericTypeTea
Well, the docs are a bit vague on how it's implemented, but it is implied that it requires a) a working network connection, b) a round-trip to the DNS server, and c) that the DNS actually knows your machine name (it doesn't have to, unless your a server)
James Curran
@James Curran - Absolutely. But Environment.MachineName restricts the computer name to a 15 character NETBIOS name. GetHostName will retrieve the full TCP/IP based hostname.
GenericTypeTea
+1  A: 

The My namespace contains many great "helper" functions like:

My.Computer.Name
Nathan