获取Windows UserPrincipal DisplayName和Change Order

时间:2018-03-27 17:38:06

标签: c#

我在获取计算机用户名时显示DisplayName的方式存在问题。目前我使用

public static string fullName = UserPrincipal.Current.DisplayName;

返回" LastName,FirstName(Department)"。

我需要以" FirstName LastName"的格式显示它们。

有推荐的方法吗?

1 个答案:

答案 0 :(得分:2)

从Microsoft.NET 3.0开始,UserPrincipal类公开了名称的部分(documented here):

using System.DirectoryServices.AccountManagement;

/* ... */

var principal = UserPrincipal.Current;
var firstname = principal.GivenName;
var middlename = principal.MiddleName;
var lastname = principal.Surname;

var customcomposite = $"{firstname} {lastname}".TrimStart();