user584018
user584018

Reputation: 11304

The network path was not found when using IP address instead of hostname of machine

Using below code, when I'm using IP Address ("159.99.222.193") of machine, I'm getting error,

When passing host name of machine, it's working good. Whats need to be done to pass IP address?

The network path was not found

at Microsoft.Win32.RegistryKey.Win32ErrorStatic(Int32 errorCode, String str) at Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(RegistryHive hKey, String machineName, RegistryView view) at Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(RegistryHive hKey, String machineName) at RegistryTest.Program.Main(String[] args) in C:\Users\h190733\source\repos\Registry\Registry\Program.cs:line 18

using (RegistryKey hive = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "159.99.222.193"))
            {

                var key = hive.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\");

                string[] names = key.GetSubKeyNames();

                foreach (string entry in names)
                {
                    Console.WriteLine(entry.ToString());
                }
            }

Upvotes: 0

Views: 1008

Answers (3)

Vikhram
Vikhram

Reputation: 4394

You can use the static variable Registry.LocalMachine instead of opening RegistryKey.OpenRemoteBaseKey. See code below

using (var key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\")) {
    string[] names = key.GetSubKeyNames();
    foreach (string entry in names) {
        Console.WriteLine(entry.ToString());
    }
}

The only reason to use RegistryKey.OpenRemoteBaseKey, is if the machine name is dynamically passed into the method - in that case, you wouldn't know you are accessing a local or a remote machine.

Upvotes: 1

matooouse
matooouse

Reputation: 98

I am a bit confuse about your question, the documentation for OpenRemoteBaseKey indicate machine name not IP.

https://learn.microsoft.com/fr-fr/dotnet/api/microsoft.win32.registrykey.openremotebasekey?view=netframework-4.7.2

I presume in this contexte that machine name is hostname.

You can get hostname from ip before and use it in your call.

Like that : How to resolve hostname from local IP in C#.NET?

Upvotes: 2

Daniel
Daniel

Reputation: 1016

You can not pass the IP address to this method as it only accepts the host name.

Instead, try fetching the hostname by IP prior to calling the method.

IPHostEntry entry = Dns.GetHostEntry(ipAddress);
var hostName = entry.HostName;

Then you can pass the hostname var into your RegistryKey.OpenRemoteBaseKey method.

You can also use the Registry.LocalMachine static variable to allow the library to fetch the hostname.

Upvotes: 2

Related Questions