System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo()中的FileNotFoundException

时间:2012-08-24 17:01:02

标签: c# .net

我有这么简单的代码:

var entry = new DirectoryEntry("WinNT://DOMAIN/MachineName, Computer");
Console.WriteLine(entry.Guid);

实际上,路径由命令行提供。编译这个简单的控制台应用程序用于测试,在我的测试中我发现:

  • 连接到我自己的Windows 7 PC。
  • 连接到网络上的任何其他Windows XP计算机,可以正常工作。
  • 连接到网络上的任何其他Windows 7计算机都失败了:
  

未处理的异常:System.IO.FileNotFoundException:找不到网络路径。

     

at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo()     在System.DirectoryServices.DirectoryEntry.RefreshCache()     在System.DirectoryServices.DirectoryEntry.FillCache(String propertyName)     在System.DirectoryServices.DirectoryEntry.get_NativeGuid()     在System.DirectoryServices.DirectoryEntry.get_Guid()     在D:\ GetDirectoryEntryProperties \ Program.cs中的GetDirectoryEntryProperties.Program.Main(String [] args):第15行

有什么想法吗?

我是所有计算机的管理员,但是我确实遇到了由设备锁定服务导致UnauthorizedAccessException审讯的另一个问题,但在这种情况下,我甚至无法读取计算机的指导。

事件日志显示没有任何用处。

2 个答案:

答案 0 :(得分:13)

我遇到了针对不同情况的相同错误消息。也许我找到的解决方案也可以帮到你。

升级到Windows 10后,我的计算机在启动时出现弹出错误,看起来就像您发布的那个错误。这是System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo()中的FileNotFoundException。

解决方案是将两个字符串从一个注册表位置复制到另一个注册表位置。

Copy these strings: RegisteredOwner and RegisteredOrganization

From: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion

To: HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion

答案 1 :(得分:0)

我只是想感谢Bryan Roach我能够解决我的问题。我也意识到我可以将我的C#项目Build设置为Platform Target x64并避免错误,因为它会搜索64位注册表区域。但是,我认为我的应用程序更适合任何CPU,程序本身能够解决问题。

string ServerName = "REMOTE_COMPUTER";
PrincipalSearcher pSearch = new PrincipalSearcher();
pSearch.QueryFilter = new UserPrincipal(new PrincipalContext(ContextType.Machine, ServerName, null, ContextOptions.Negotiate));

try
{
    foreach (UserPrincipal userUP in pSearch.FindAll())
    {
         //Missing Registry Keys will error on pSearch.FindAll();
         //Either Build > Platform Target == x64 or deal with it.
    }
}
catch(FileNotFoundException ex)
{
    if(ex.Source.Equals("Active Directory") && 
       ex.TargetSite.MemberType.ToString().Equals("Method") && 
       ex.TargetSite.Name.Equals("GetInfo"))
    {
        //It's possible the registry keys haven't been moved to x86 location on a 64 bit machine:
        //From: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion (64 bit)
        //To: HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion (32 bit compatability area)
        //String Properties need to be present:  RegisteredOwner, RegisteredOrganization
        try
        {
            Hack_Fixx64RegistryForGettingLocalAccounts(ServerName);
            //Recall function or whatever to try again with fixed registry.
        }
        catch
        { }
    }
}

然后,将注册表项复制到正确位置的函数:

private void Hack_Fixx64RegistryForGettingLocalAccounts(string ServerName)
{
    RegistryKey remoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ServerName, RegistryView.Registry64);
    if(remoteKey != null)
    {
        //Get keys stored on 64 bit location
        RegistryKey x64regkey = remoteKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion");
        string regOwner = Convert.ToString(x64regkey.GetValue("RegisteredOwner", ""));
        string regOrganization = Convert.ToString(x64regkey.GetValue("RegisteredOrganization", ""));
        //Add missing keys on 64 bit OS in correct location for 32 bit registry area. The Wow6432Node is for 32-bit apps that run on 64-bit window versions.
        remoteKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ServerName, RegistryView.Registry32);
        if(remoteKey != null)
        {
            RegistryKey x86regkey = remoteKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", true);
            x86regkey.SetValue("RegisteredOwner", regOwner);
            x86regkey.SetValue("RegisteredOrganization", regOrganization);
        }
    }
}