System.IO.FileNotFoundException: The network path was not found. Exception while using DirectoryEntry object on windows 7

sagar picture sagar · Dec 19, 2012 · Viewed 7.3k times · Source

i am trying to connect to remote windows 7 machine by using DirectoryEntry object. here is my code

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

if i am able to connect to remote windows server 2003 machine or local windows 7 machine , then i didn't get any error. But when i am trying to connect to remote windows 7 machine then i gets exception at line 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()

I am able to ping that remote windows 7 m/c. firewall and UAC is also off. Problem is i am getting this problem on each remote windows 7 machine. I have tried to put ip adress in url instead of hostname, but in that case even on local machine i get above exception. nslookup command is able to resolve that remote m/c hostName. What may be reason for this ? What can be possible solution for this ? Thanks in advance !!

Answer

Petesh picture Petesh · Dec 19, 2012

You need proper access to the remote registry to be able to connect to Windows 7 (&8) systems in this manner.

There is a guide here on how to successfully connect to the remote registry. It gathers all the disparate information needed - enabling file and printer sharing, enabling the remote registry service and adding the specific user that is connecting to the remote registry into the HKLM\SYSTEM\CurrentControlSet\Control\SecurePipeServers\winreg.

Once all these tasks have been performed on the remote system, you should be able to connect remotely to it using WinRT://remoteHost. I tested it on some systems I had and it works.

This is would probably all work automatically if the system was a domain bound system and the account being used to connect was a domain adminstrator account.