Environment.MachineName truncates after the first 15 characters

When in a domain, a machine’s name is limited to 15 characters, but in a workgroup a machine name can be much longer. However, as one of our internal customers noticed, Environment.MachineName truncates after the first 15 characters.  This is because Environment.ComputerName maps to NetBIOS name of the local computer and not the DNS host name. NetBIOS name is limited to MAX_COMPUTERNAME_LENGTH which is 15 for Windows (refer to winbase.h).

 

You might want to look into Dns.GetHostByName or P/Invoke to GetHostNameEx for more flexibility.

 

 

Published 10 August 05 09:28 by BradA
Filed under: ,

Comments

# Keyvan Nayyeri said on August 11, 2005 2:44 AM:
Good point.
Probably we can use a class to ensure about this by checking the lenght.
# Grim said on August 11, 2005 9:14 AM:
I have a really stupid question here.

Why not just fix the bug instead of using workarounds?
# BradA said on August 11, 2005 10:40 AM:
Thanks Grm -- I am not sure that I think of this as a bug.. it is the way we designed it.. that said, if it is tripping people up, i'd love to hear it..
# Ravi Akula said on August 11, 2005 3:02 PM:
Well then call it a "Design Bug" and fix it.Certainly truncation is not cool and that too 15 chars? thats too small to...at least if you can increase it to 50 chars then it might be OK...
# Willy Denoyette - MVP said on August 11, 2005 3:43 PM:
Environment.MachineName returns what is found in the environment variable "Computername, which is the NetBIOS name of the machine. This has been like this since ages and should remain like this if you don't wanna break a zillion of applications. As Brad said, there is nothing to fix, you just need to call the right API, things like GetHostName come to mind but also the System.Management and the System.Directory namepaces do both have API's to get the correct hostname be it in a domain or in a workgroup.
# Christopher Steen said on August 11, 2005 11:29 PM:
ASP.NET Podcast #10 - Sahil Malik and Hilary Cotter [Via: http://www.scalabledevelopment.com/mcclure.htm...
New Comments to this post are disabled

Search

Go

This Blog

Syndication

Page view tracker