69

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

2
  • Why have you yags vb.net when the title says c#. Commented Aug 16, 2010 at 16:01
  • 3
    C# or VB.NET ... Any would be accepted Commented Aug 16, 2010 at 16:28

5 Answers 5

112

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 - Absolutely. But Environment.MachineName restricts the computer name to a 15 character NETBIOS name. GetHostName will retrieve the full TCP/IP based hostname.
System.Environment.GetEnvironmentVariable("COMPUTERNAME"); is Windows only
Will this still work if the code is running on IIS? What will it return? something like "www.mySiteAddress.com" ??
39

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

System.Environment.MachineName

Comments

12

To get fully qualified name, use:

 System.Net.Dns.GetHostEntry("").HostName

Comments

4

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

My.Computer.Name

1 Comment

This appears to be limited to VB.NET and is proprietary to that language: msdn.microsoft.com/en-us/vstudio/ms789188.aspx
2
System.Windows.Forms.SystemInformation.ComputerName;

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.