1

I'm trying to throw together a little .Net (Framework, v4.8) Console Application to check, and potentially update, registry keys on our domain computers. The code I've written work fine on my development machine (Windows 10) - both within Visual Studio and running stand-alone - but refuses to work on our projection servers.

The code is fairly simple - the problem line is:

  var remotereg = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName, RegistryView.Default);

This always returns "null" (again, it's fine on my development machine).

Following the advice in this answer, I've wrapped the request in a NetworkConnection object to pass through credentials.

   using (new NetworkConnection(@"\\" + machineName + @"\admin$", new NetworkCredential(@"mydomain\myaccount", "mypassword")))
   {
       var remotereg = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, machineName, RegistryView.Default);
   }

With this, I get the slightly-more helpful exception

 The network name cannot be found

This is all running from an Administrator account, from a UAC'd Command Prompt (i.e. with Administrator permissions). From that very same prompt, I can ping the machineName I'm trying to access, and even

>net use \\machineName\admin$ /user:mydomain\myaccount

The command completed successfully

and

>dir \\machineName\admin$
 Volume in drive \\machineName\admin$ has no label.
 Volume Serial Number is C28A-5784

 Directory of \\machineName\admin$

18/09/2022  21:44    <DIR>          .
18/09/2022  21:44    <DIR>          ..
22/08/2013  16:39    <DIR>          ADFS
13/10/2019  11:24    <DIR>          AppCompat

... both suggest that the server I'm running the code from can access the remote server fine - it can resolve the name, it's authenticated, and I can browse file and admin shares. What do I need to do to be able to use OpenRemoteBaseKey?

KenD
  • 5,280
  • 7
  • 48
  • 85
  • Are you an admin on both local and remote machine? Needs to be the same network account using the same password server (or server group). – jdweng Oct 12 '22 at 14:17
  • @jdweng: yes, I'm using the same account on both my development machine and the server I'm testing on. The account is a domain admin. – KenD Oct 12 '22 at 14:21
  • If running inside VS than you need to start VS by right click VS shortcut and select Run As admin. You also do not need credentials. Will automatically use the login credentials. You are not adding credentials when it works – jdweng Oct 12 '22 at 14:24
  • @jdweng: it works fine within VS (even without specifying credentials). The "release" binary always works fine on my machine. It's only an issue when running this on any other machine. – KenD Oct 12 '22 at 14:27
  • Did you check the event viewer on the local and remove machines? I would try a simple command like DIR to make sure your credentials are working. – jdweng Oct 12 '22 at 14:49
  • @jdweng: please see my original post - `dir` and `net use` both work fine. – KenD Oct 12 '22 at 15:00
  • Make a folder on remote machine that only an admin can access. Then try DIR again. Lets see if you have admin rights on remote machine. – jdweng Oct 12 '22 at 15:09

0 Answers0