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.
-
3Well, 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) Aug 16, 2010 at 16:09
-
1@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.– djdd87Aug 16, 2010 at 16:21
-
4
System.Environment.GetEnvironmentVariable("COMPUTERNAME");
is Windows only– gmanJan 28, 2016 at 3:10 -
Will this still work if the code is running on IIS? What will it return? something like "www.mySiteAddress.com" ??– AleX_Aug 4, 2017 at 19:42
The My namespace contains many great "helper" functions like:
My.Computer.Name
-
7This appears to be limited to VB.NET and is proprietary to that language: msdn.microsoft.com/en-us/vstudio/ms789188.aspx Sep 7, 2012 at 17:37
-