使用System.DirectoryServices.AccountManagement获取FileNotFoundException

时间:2016-04-06 22:58:07

标签: c# exception active-directory credentials

我有以下代码来获取当前登录用户的完整显示名称:

using System.DirectoryServices.AccountManagement;

public class Form1
{
    private void Button1_Click(object sender, EventArgs e)
    {
        var up = UserPrincipal.Current;
        var dn = up.DisplayName;
    }
}

问题是它在var up = UserPrincipal.Current;行失败并出现FileNotFoundException。地球上到底发生了什么?我在未连接到域的计算机上使用Windows 10。我使用.NET FW 4.5.1。

这是一个堆栈跟踪

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()

0 个答案:

没有答案