I need to get the host name currently running the application. Any idea?
5 Answers
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.
4 Comments
James Curran
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)
djdd87
@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.
AleX_
Will this still work if the code is running on IIS? What will it return? something like "www.mySiteAddress.com" ??
The My namespace contains many great "helper" functions like:
My.Computer.Name
1 Comment
Dan Esparza
This appears to be limited to VB.NET and is proprietary to that language: msdn.microsoft.com/en-us/vstudio/ms789188.aspx
System.Windows.Forms.SystemInformation.ComputerName;
1 Comment
Jonas
Looks like this one also gets the NetBIOS name, so the same as
System.Environment.MachineName and avoiding a potentially awkward System.Windows.Forms situation.