Link to home
Start Free TrialLog in
Avatar of JameMeck
JameMeckFlag for United States of America

asked on

Delete specific keys from registry with C#.

I want to delete following keys with C# but I don't know how to do.

Could you help me?

Key: "Registed"
Path: "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"

Key: "Logedon"
Path: "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Check:
http://stackoverflow.com/questions/531151/how-to-delete-registry-value-in-c-sharp

string keyName = @"Your key path here"; 
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true)) 
{ 
    if (key == null) 
    { 
        // Key doesn't exist. Do whatever you want to handle 
        // this case 
    } 
    else 
    { 
        key.DeleteValue("MyApp"); 
    } 
} 

Open in new window

Avatar of JameMeck

ASKER

I tried your way, but I cannot delete the keys.

Please take a look the attached picture.

Thanks!
Register.png
What is the error message and the path you use?
Also what OS? and
Is it 32 or 64 bit machine?
Note: Post your code and details of the error and/or exception message.
I am running on Windows Server 2003 Standard 32 Bit.

My Code:

string keyName = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon";
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true))
            {
                if (key == null)
                {
                    // Key doesn't exist. Do whatever you want to handle 
                    // this case 
                }
                else
                {
                    key.DeleteValue("Registed");
                }
            } 

Open in new window


The registry key is exist (attached file).

But when I run the command, it says key not exist.
Error.png
Register.png
are you running this code as Administrator or have read/write permissions to this key?
Check: http://superuser.com/questions/185434/why-cannot-administrators-delete-registry-keys-when-they-run-regedit-as-administ

Can you provide full registry path or confirm the path below?
Notice that I am using CurrentUser, thus the full path should be like:
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon

Can you deleted the key manually?
Dear Mas,

I tried these codes but cannot:

 string keyName = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon";
            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true))
            {
                if (key == null)
                {
                    MessageBox.Show("The key does not exist");
                }
                else
                {
                    key.DeleteValue("Registed");
                    key.DeleteValue("Logedon");
                }
            } 

Open in new window



I can delete them by hand.
ASKER CERTIFIED SOLUTION
Avatar of JameMeck
JameMeck
Flag of United States of America image

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
I found my solution.