调用UserPrincipal.FindByIdentity时出现奇怪的.Net 4,System.IO.FileNotFoundException,

时间:2016-04-25 06:52:48

标签: c# active-directory windows-10 runtimeexception

我的系统从win7 x64升级到10 x64后,我开始遇到一个新的运行时 System.IO.FileNotFoundException 异常。

我有以下代码,到目前为止完美无缺:

{
    PrincipalContext pc = new PrincipalContext(ContextType.Machine, Environment.MachineName);
    UserPrincipal usrPrin = UserPrincipal.FindByIdentity(pc, IdentityType.SamAccountName, Username);

    if (usrPrin == null) // No user exist
    {
        return -1;
    }
    // user exist 
    return 1;                    

}

我总是使用平台目标构建项目:x86,当我运行应用程序时,我在调用 UserPrincipal.FindByIdentity 时得到异常。

  

类型'System.IO.FileNotFoundException'的第一次机会异常   发生在System.DirectoryServices.dll中的第一次机会异常   类型'System.IO.FileNotFoundException'发生在   System.DirectoryServices.AccountManagement.dll

我的所有项目都使用.net4编译,而不是.net4.5

在我现在的调试过程中,我发现如果我将构建平台目标更改为AnyCPU,则代码传递没有问题。

这似乎是一个操作系统错误! .Net 4 x86没有安装或者什么不对!我尝试重新安装.Net4但不能因为它已经安装了,伊朗.net清理工具。

任何想法为什么会出现这种异常?

1 个答案:

答案 0 :(得分:0)

它表示程序集System.DirectoryServices.dll丢失。您是否有机会验证当前解决方案中是否存在dll。如果没有尝试下载nuget,请构建并查看。我遇到了类似的问题,下载了System.DirectoryServices dll后,它在vs 2017和.net core 2.1中得到解决