c# - System.IO.FileNotFoundException : The network path was not found. 在 Windows 7 上使用 DirectoryEntry 对象时出现异常

标签 c# .net directoryentry

我正在尝试使用 DirectoryEntry 对象连接到远程 Windows 7 机器。这是我的代码

DirectoryEntry obDirEntry = new DirectoryEntry("WinNT://hostName", "hostName\\testUser", "password123", AuthenticationTypes.Secure);               
            try
            {
                if (obDirEntry.Properties.Count > 0)
                {                        
                   //ok
                }
            }
            catch (Exception excp)
            {}

如果我能够连接到远程 Windows Server 2003 机器或本地 Windows 7 机器,那么我没有收到任何错误。但是当我尝试连接到远程 Windows 7 机器时,我在 if (obDirEntry.Properties.Count > 0)

行出现异常
 Exception :System.IO.FileNotFoundException: The network path was not found.

 at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.GetInfo()
 at System.DirectoryServices.DirectoryEntry.RefreshCache()
 at System.DirectoryServices.DirectoryEntry.FillCache(String propertyName)
 at System.DirectoryServices.PropertyCollection.get_Count()

我能够 ping 通远程 Windows 7 m/c。防火墙和UAC也关闭了。问题是我在每台远程 Windows 7 机器上都遇到了这个问题。我试图将 ip 地址放在 url 而不是主机名中,但在那种情况下,即使在本地机器上我也会遇到异常。 nslookup 命令能够解析远程 m/c 主机名。 这可能是什么原因?有什么可能的解决方案? 提前致谢 !!

最佳答案

您需要正确访问远程注册表才能以这种方式连接到 Windows 7 (&8) 系统。

有一个guide here关于如何成功连接到远程注册表。它收集所有需要的不同信息 - 启用文件和打印机共享,启用远程注册表服务并将连接到远程注册表的特定用户添加到 HKLM\SYSTEM\CurrentControlSet\Control\SecurePipeServers\winreg.

在远程系统上执行完所有这些任务后,您应该能够使用 WinRT://remoteHost 远程连接到它。我在我拥有的一些系统上对其进行了测试,它可以正常工作。

如果系统是域绑定(bind)系统并且用于连接的帐户是域管理员帐户,这可能会自动完成。

关于c# - System.IO.FileNotFoundException : The network path was not found. 在 Windows 7 上使用 DirectoryEntry 对象时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13955880/

相关文章:

c# - 从 ASP.NET Core 2 Controller 返回 PDF

c# - 类未注册(Windows Phone 8.1 中的 HRESULT : 0x80040154) when BackgroundExecutionManager. RequestAccessAsync 异常

c# - 使用正则表达式拆分字符串

c# - 内置类型的协方差

c# - 我可以加快此查询以检索域中的所有计算机吗?

c# - 为什么 C# CreateObject 比 VB.NET 冗长得多?

c# - 获取 “Could not find part of the path” 错误

c# - 在 .NET 4 中编译 .NET 代码?

C# 尝试创建带空格的组织单位时出错

c# - 设置 Windows/AD 密码,使其成为 "never expires"?