views:

561

answers:

2

Is there a .NET equivalent to the winsock function GetNameInfoW() with the NI_NOFQDN flag set? As the MSDN docs for GetNameInfoW explain:

Setting the NI_NOFQDN flag results in local hosts having only their Relative Distinguished Name (RDN) returned in the pNodeBuffer parameter.

The closest thing I can find is System.Net.Dns.GetHostEntry(), which returns an IPHostEntry but whose HostName is the fully-qualified domain name of the host. I just want the unqualified host name.

For example, if IP address "x.x.x.x" resolves to the FQDN "foohost.company.domain.com", I can get "foohost" from GetNameInfoW() by supplying NI_NOFQDN, but there doesn't seem to be an equivalent in .NET. I'm not sure if GetNameInfoW() is doing NetBIOS or LDAP or something else under the covers. Any thoughts on how to do the same in .NET?

+1  A: 

Are you trying to get the computer's name, or the DNS name for the computers IP (sometimes different). If you really wanted to use GetNameInfoW() you could always just use Pinvoke from .NET.

Edit: This might be a hack, but could you use the result from System.Net.Dns.GetHostEntry() and just do a string.Split('.') and take index 0 as the unqualified name?

Jon Tackabury
I think what GetNameInfoW() provides is the computer's name (but I also thought that was ultimately part of a fully-qualified name for DNS purposes. Presently I am calling GetNameInfoW() from .NET, and I'd prefer not to (for simpler IPv6 compatibility among other things).
Jason
+1  A: 

If the computer has multiple IP-Adresses, it can have multiple hostnames as well (fully qualified or not doesn't matter in this context).

If I needed the "computer name" I used "Environment.MachineName" in the past, which technically returns the NetBIOS name of the box, but at least that is not dependend on the IP configuration.

Christian.K