0

I'm having an issue with the way a DisplayName is shown when getting the name of a computer user. Currently I use

public static string fullName = UserPrincipal.Current.DisplayName;

which returns "LastName, FirstName (Department)".

I need to have them displayed in the format of "FirstName LastName".

Is there a recommended way to do this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
SomethingStrange
  • 121
  • 1
  • 12
  • 1
    Have you looked at the `Name` property? –  Mar 27 '18 at 17:40
  • You can split the value by comma and have first item as last name and determine the first name by getting SubString from 0 to the first index of `(` in the second item. – Chetan Mar 27 '18 at 17:41
  • this is for a reason. mainly for lookup when sorted alphabetically. in your program, you are free to query for `sn` and `givenName` and compose them as you see fit, via LDAP. .NET has wrappers around this starting in version 3.0, for example [here](https://stackoverflow.com/a/3471954/1132334) and [here](https://stackoverflow.com/a/21699904/1132334) – Cee McSharpface Mar 27 '18 at 17:42

1 Answers1

2

Starting in Microsoft.NET 3.0, the UserPrincipal class exposes the parts of the name (as 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();
Cee McSharpface
  • 8,493
  • 3
  • 36
  • 77