Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is System.Environment.MachineName value uppercased?

My machine name is lowercased (i see that in Advanced system settings dialog, Computer Name tab) but System.Environment.MachineName reports it uppercased. Why is that? This is a real problem for me because from my tests PrincipalPermissionAttribute performs case sensitive comparison for role names (i map my custom roles to Windows groups and my environment is non-domain). Any advise?

like image 480
UserControl Avatar asked Dec 18 '10 13:12

UserControl


People also ask

How do I get the operating system information from the environment class?

The Environment class has a huge array of information and accessing this information is very simple, I’m going to try and give a whole bunch of one line examples of how to do this using PowerShell. To retrieve the comptuer name Get Operating System Information (such as Platform, service pack, version, and version string)

What is the value of strmachinename in the environment variable?

The value of strMachineName is the same as the value of the COMPUTERNAME environment variable. The following example demonstrates how to get the value of the COMPUTERNAME environment variable.

What are the namespaces of a system assembly?

Namespace: System Assembly: System.Runtime.dll Assembly: System.Runtime.Extensions.dll Assembly: mscorlib.dll Assembly: netstandard.dll Important Some information relates to prerelease product that may be substantially modified before it’s released.

How is the name of a computer established?

The name of this computer is established at system startup when the name is read from the registry. If this computer is a node in a cluster, the name of the node is returned.


2 Answers

The source for Environment.MachineName for .NET 4.7.1 is here: https://referencesource.microsoft.com/#mscorlib/system/environment.cs,be0b5c103d248dce

It p/invokes GetComputerName as seen here: https://referencesource.microsoft.com/#mscorlib/microsoft/win32/win32native.cs,0c7d7f4f83d4ddd0

Here is the GetComputerName function: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724295(v=vs.85).aspx, which states:

GetComputerName retrieves only the NetBIOS name of the local computer. To retrieve the DNS host name, DNS domain name, or the fully qualified DNS name, call the GetComputerNameEx function.

The MSDN for Computer Names, https://msdn.microsoft.com/en-us/library/ms724220(VS.85).aspx, states:

NetBIOS names consist of up to 15 bytes of OEM characters including letters, digits, hyphens, and periods. Some characters are specific to the character set. NetBIOS names are typically represented in the OEM character set. The OEM character set depends on the locale. Some OEM character sets represent certain characters as two bytes. NetBIOS names, by convention, are represented in uppercase where the translation algorithm from lowercase to uppercase is OEM character set dependent.

So, NetBIOS names are upper case by convention and System.Environment.MachineName returns the system's NetBIOS name.

like image 94
Quantic Avatar answered Sep 28 '22 02:09

Quantic


Use Dns.GetHostName instead, that should return it with the correct case (at least it does on my computer).

like image 31
Hans Olsson Avatar answered Sep 28 '22 01:09

Hans Olsson