我需要获取当前用户的显示名称,并且无法找到始终有效的解决方案。为了清楚起见,我没有找到用户名。我需要" John Doe"。 “开始”菜单上显示的值。
关于这个问题有很多帖子,但没有一个能解决我的问题。
How do I get the AD Display Name of the currently logged in user
这两篇文章引导我:
PrincipalContext context = domain.Equals(Environment.MachineName, StringComparison.CurrentCultureIgnoreCase) ?
new PrincipalContext(ContextType.Machine) :
new PrincipalContext(ContextType.Domain, domain);
UserPrincipal userPrincipal = new UserPrincipal(context) { SamAccountName = username };
PrincipalSearcher searcher = new PrincipalSearcher(userPrincipal);
userPrincipal = searcher.FindOne() as UserPrincipal;
string displayName = userPrincipal.DisplayName;
这段代码大部分都适用。但是,如果用户在他/她的计算机上禁用/停止了服务器服务,我会收到一个例外,说“"服务器服务未启动。”#34;
System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName
同样的错误。
How to get logged-in user's full name in windows?
StringBuilder name = new StringBuilder(1024);
uint userNameSize = (uint)name.Capacity;
const int NameDisplay = 3;
GetUserNameEx(NameDisplay, name, ref userNameSize)
如果用户不在域中,则返回无错误,但返回空字符串。
How do you read the user's display (first and last) name on all versions of Windows reliably?
// get SAM compatible name <server/machine>\\<username>
if (0 != GetUserNameEx(2, username, ref userNameSize))
{
IntPtr bufPtr;
try
{
string domain = Regex.Replace(username.ToString(), @"(.+)\\.+", @"$1");
DirectoryContext context = new DirectoryContext(DirectoryContextType.Domain, domain);
DomainController dc = DomainController.FindOne(context);
if (0 == NetUserGetInfo(dc.IPAddress,
Regex.Replace(username.ToString(), @".+\\(.+)", "$1"),
10, out bufPtr))
{
var userInfo = (USER_INFO_10) Marshal.PtrToStructure(bufPtr, typeof (USER_INFO_10));
return Regex.Replace(userInfo.usri10_full_name, @"(\S+), (\S+)", "$2 $1");
}
}
finally
{
NetApiBufferFree(out bufPtr);
}
}
通过上面的内容,我得到一个ActiveDirectoryObjectNotFoundException,消息为&#34;在域中找不到域控制器。&#34;当调用DomainController.FindOne时。
我还没找到显示名称的注册表设置。
我不知道还有什么可以尝试的。请帮忙。
答案 0 :(得分:4)
以上所有方法仅适用于域名。如果不是,则必须依赖本地用户帐户存储。以下详细说明了如何检索此信息:How can I get a list Local Windows Users (Only the Users that appear in the windows Logon Screen)。但在域状况下,用户帐户将不在本地存储中。
如果您在域中但未连接到域控制器,则无法随时使用显示名称。此信息存储在域控制器上,而不是本地用户的计算机上。如果您的用户位于域中,他们实际上应该无法禁用服务器服务(使用GPO)。此外,他们失去的功能远远超过通过禁用该服务来检索其用户帐户的能力。
在尝试获取显示名称之前,我会检查域可用性。如果失败,则显示指示失败的消息。这里可能存在太多边缘情况,使其适用于所有这些情况。使用您打算在其下使用该程序的场景,并为其他人提供错误消息。