I need to get the host name currently running the application. Any idea?
Asked
Active
Viewed 1.2e+01k times
59
-
Why have you yags vb.net when the title says c#. – Ash Burlaczenko Aug 16 '10 at 16:01
-
2C# or VB.NET ... Any would be accepted – Beginner_Pal Aug 16 '10 at 16:28
5 Answers
99
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.

djdd87
- 67,346
- 27
- 156
- 195
-
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) – James Curran Aug 16 '10 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. – djdd87 Aug 16 '10 at 16:21
-
5`System.Environment.GetEnvironmentVariable("COMPUTERNAME");` is Windows only – gman Jan 28 '16 at 03:10
-
Will this still work if the code is running on IIS? What will it return? something like "www.mySiteAddress.com" ?? – AleX_ Aug 04 '17 at 19:42
4
The My namespace contains many great "helper" functions like:
My.Computer.Name

Nathan
- 431
- 2
- 10
-
7This appears to be limited to VB.NET and is proprietary to that language: http://msdn.microsoft.com/en-us/vstudio/ms789188.aspx – Dan Esparza Sep 07 '12 at 17:37
-
2
System.Windows.Forms.SystemInformation.ComputerName;

Nobody
- 4,731
- 7
- 36
- 65
-
1Looks like this one also gets the NetBIOS name, so the same as `System.Environment.MachineName` and avoiding a potentially awkward System.Windows.Forms situation. – Jonas Aug 16 '22 at 11:47