I have the following code to get the full display name of the current user:
using System.DirectoryServices.AccountManagement;
public class Form1
{
private void Button1_Click(object sender, EventArgs e)
{
var up = UserPrincipal.Current;
var dn = up.DisplayName;
}
}
The problem is that this fails on a var up = UserPrincipal.Current;line with a FileNotFoundException. What's going on here? I am on Windows 10 on a machine that is not connected to a domain. I am using .NET FW 4.5.1.
Here's the stack trace
System.IO.FileNotFoundException: The system cannot find the file specified.
at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo()
at System.DirectoryServices.DirectoryEntry.RefreshCache()
at System.DirectoryServices.AccountManagement.PrincipalContext.DoMachineInit()
at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable'1 identityType, String identityValue, DateTime refDate)
at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, IdentityType identityType, String identityValue)
at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue)
at System.DirectoryServices.AccountManagement.UserPrincipal.get_Current()
source
share