Sunday, August 24, 2008

Getting machine name in .net

Environment.MachineName is commonly used to get the machine name in .net. Few days ago, I met a problem with it after renaming a machine and joining a domain: Environment.MachineName returned the old name machine name.

While searching for the reason, I found that Environment.MachineName gets the NetBIOS name, which is stored in the registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Contro l\ComputerName\ComputerName\ComputerName and reflects in the Computername envronment variable.

A big limitation of NetBIOS name in this case is that it's limited to the first 15 characters of the machine name. An alternative is needed to get the full name. Which is System.Net.Dns.GetHostName() method which gets the DNS host name of the local computer, which is not limited to the first 15 characters.

Back to my problem, I edited the registry key to reflect the new machine name. That was good enough for me!!

No comments: