[DllImport("secur32.dll", CharSet=CharSet.Auto)]
public static extern int GetUserNameEx (int nameFormat, StringBuilder userName,
ref uint userNameSize);
Declare Function GetUserNameEx Lib "secur32.dll" (nameFormat As Integer, _
userName As StringBuilder, ByRef userNameSize As Integer) As Integer
None.
This may be required because System.Environment.UserDomainName is broken. If the local machine has a user account that is the same name as a logged in domain user (machineName\bob & domainName\bob) UserDomainName returns the machine name, not the domain name.
Please add some!
public class Sample
{
enum EXTENDED_NAME_FORMAT
{
NameUnknown = 0,
NameFullyQualifiedDN = 1,
NameSamCompatible = 2,
NameDisplay = 3,
NameUniqueId = 6,
NameCanonical = 7,
NameUserPrincipal = 8,
NameCanonicalEx = 9,
NameServicePrincipal = 10,
NameDnsDomain = 12
}
[DllImport("secur32.dll", CharSet=CharSet.Auto)]
public static extern int GetUserNameEx (int nameFormat, StringBuilder userName, ref int userNameSize);
public String GetUserDomain()
{
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
return null;
StringBuilder userName = new StringBuilder(1024);
int userNameSize = userName.Capacity;
if(0 != GetUserNameEx((int)EXTENDED_NAME_FORMAT.NameSamCompatible, userName, ref userNameSize))
{
string[] nameParts = userName.ToString().Split('\\');
if (2 != nameParts.Length) return null;
return nameParts[0];
}
return null;
}
}
System.Environment.UserDomainName