Link to home
Start Free TrialLog in
Avatar of kentrg11
kentrg11

asked on

Delete Registry Key on a remote computer

I just want to delete a registry Key on a remote Computer5, the registry key is

HKEY_LOCAL_MACHINE\SOFTWARE\Testing

I have the code in place but keep getting "cannot write to Registry" althought i have full admin and registry rights on this PC.

Here is the code
string Computer5 = TextBox2.Text; 

Open in new window



RegistryKey key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, Computer5).OpenSubKey("Software");
                key.DeleteSubKey("Testing");
                key.Close();

Open in new window


this is domain enviroment
Avatar of kentrg11
kentrg11

ASKER

any help/leads appreticated
RegistryKey myKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "computername");
            myKey.DeleteSubKey(@"SOFTWARE\test");

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of piattnd
piattnd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In my example, I'm using 2 RegistryKey variables, but you don't need to.  Just add the ",true" to your opensubkey section and you should be fine.
I have this code below as you've advised but im getting the follow error message
"Cannot delete a subkey tree because the subkey does not exist."
i confirmed there is a key on computerName called SOFTWARE/Testing

RegistryKey myKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, computerName);
                RegistryKey mySubKey = myKey.OpenSubKey("SOFTWARE", true);
                mySubKey.DeleteSubKey("Testing");

Open in new window

Can you confirm that the Testing key in Software is in the HKLM not the HKCU location?
just copy and pasted  this from the remote Computer im trying to access
HKEY_LOCAL_MACHINE\SOFTWARE\Testing

the code again is

RegistryKey myKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, computerName);
                RegistryKey mySubKey = myKey.OpenSubKey("SOFTWARE", true);
                mySubKey.DeleteSubKey("Testing", true);
                myKey.Close();

Open in new window

figured it out , had to add RegistryView.Registry64);
excellent
Sorry I was so late in getting back to this.  Glad you found the missing piece!  Good luck!