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.


 


 

Comments (6)

  1. Good point.

    Probably we can use a class to ensure about this by checking the lenght.

  2. Grim says:

    I have a really stupid question here.

    Why not just fix the bug instead of using workarounds?

  3. BradA says:

    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..

  4. Ravi Akula says:

    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…

  5. Willy Denoyette - MVP says:

    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.

  6. ASP.NET Podcast #10 – Sahil Malik and Hilary Cotter [Via: http://www.scalabledevelopment.com/mcclure.htm