System. environment. userdomainname:
Obtain the network domain name associated with the current user.
There is a note below:
The value of this attribute is usually the host name, but may depend on the deployed application.ProgramSolution. The current user is the name specified in username.
Generally, if I add a domain, the current domain is returned. If not, the current machine name is returned.
Generally, this is correct. Sometimes, you will find that even if you are added to the domain, the returned machine name is used.
I found that there is a reason for this. userdomainname actually calls a lookupaccountname, and the explanation of this API is not clear.
Internal Static Extern Bool Lookupaccountname ( String Machinename, String Accountname, Byte [] Sid, Ref Int Sidlen, stringbuilder domainname, Ref Int Domainnamelen, Out Int Peuse );
About domainname,
Referenceddomainname
[Out] pointer to a buffer that determines es the name of the domain where the account name is found. for computers that are not joined to a domain, this buffer takes es the computer name. if this parameter is null, the function returns the required buffer size.
The server name is returned even if you are added to the domain.
Later we found the rule. If the domain user has the same user name on the local machine, the local machine name will be returned. In this case, it is okay to delete the local user.