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

我有这么简单的代码:

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

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

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

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

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

有任何想法吗?

我是所有机器的管理员,但是我确实有另一个由Device Lock服务引起的问题,它在询问时引起了UnauthorizedAccessException ,但是在这种情况下我甚至无法读取机器的Guid。

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

卢克

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

升级到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 

我只是想感谢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 { } } } 

然后,为了将注册表项复制到正确位置的function:

 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); } } }