❔ Delete a registry DWORD with just one button for visual studio c#
How do i delete a registry dword with clicking a button in visual studio?
155 Replies
Where is the reg key located?
And you mean button in visual studio? Or in forms or wpf?
in visual studio and the registry key i want to delete is a test key.
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip DWORD: what that is the dword i want to delete
Computer\HKEY_LOCAL_MACHINE\SOFTWARE\7-Zip DWORD: what that is the dword i want to delete
You might need admin privileges to delete a key in HKLM
it has admin privileges but this is the script that doesnt work:
wait a minute
lemme find my script
Sure
private void buttonDeleteRegistry_Click(object sender, EventArgs e)
{
const string registryKey = @"SOFTWARE\7-Zip";
const string valueName = "what";
try
{
// Open the registry key
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(registryKey, true))
{
// Check if the key exists
if (key != null)
{
// Delete the value
key.DeleteValue(valueName);
MessageBox.Show("Registry value deleted successfully.");
}
else
{
MessageBox.Show("Registry key does not exist.");
}
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message);
}
}
You're searching in the wrong hive hkcu
Registry.CurrentUser
oh
so i change it to localmachine?
Yes
TY
ill tell u if its fixed or not
Yea
LocalMachine
Make sure L and M are capital
yea
i think i got to restart my pc for it to take effect
brb
it says that it deleted the registry dword
and then it says that the value doesnt exist
but the dword is still here
Gimme 10 mins
alr ty
Aight
Soo what's happening is you're deleting the value of what
Not the subkey itself
Soo do this instead
oh
Key.DeleteSubKey(valuename);
wait
where do i put it
it has an error
wait nvm
i fixed it
ty again
ima test it
valueName
Yea go ahead
Can you show me the whole tree?
I mean the registry tree
ok
private void button20_Click_1(object sender, EventArgs e)
{
const string registryKey = @"SOFTWARE\7-Zip";
const string valueName = "what";
try
{
// Open the registry key
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey, true))
{
// Check if the key exists
if (key != null)
{
// Delete the value
key.DeleteSubKey(valueName);
MessageBox.Show("Registry value deleted successfully.");
}
else
{
MessageBox.Show("Registry key does not exist.");
}
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred: " + ex.Message);
}
}
}
}
My code
Alright, 1 min
ty
Ideally this should work
Do one thing instead of deleting
Get the value of the subkey
Object value = Key.GetValue(valueName);
MessageBox.Show(value);
right here right?
Yup
Just comment them out
alr ty
Instead of object make it var value
alr
it still has the error
Errr give me 5 mins imma test it on my machine
alr ty
Alright just tested it out
alr
It's key.DeleteValue();
where do i put it
Remove these 2 lines
And replace key.deletesubkey with deletekey
My bad you were doing the right thing all along
is it this?
Exactly
I don't see any reason why this won't work
alr
ima test it again
thats really weird
Do one thing
Instead of variables just keep the paths
where
private void button20_Click_1(object sender, EventArgs e)
{
const string registryKey = @"SOFTWARE\7-Zip";
const string valueName = "what2";
try
{
// Open the registry key
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey, true))
{
// Check if the key exists
if (key != null)
{
// Delete the value
key.DeleteValue(valueName);
MessageBox.Show("Registry value deleted successfully.");
} else { MessageBox.Show("Registry key does not exist."); } } } catch (Exception ex) { MessageBox.Show("An error occurred: " + ex.Message); } } } }
} else { MessageBox.Show("Registry key does not exist."); } } } catch (Exception ex) { MessageBox.Show("An error occurred: " + ex.Message); } } } }
Instead of opensubkey(variable)
Just keep that path there
Opensubkey (the path)
alr ty
this right?
Keep the , true as well after path
And instead of valueName keep the actual name of the key
Let's do it without variables once
alr
ima test it
Sure
Do one thing
Create a key in HKCU hive
And change the registry.LocalMachine to registry.CurrentUser
ok
i will create it here
wait
nvm
here
i will create it here
Sure
I'd suggest you create a dummy subkey in HKCU and try all sorts of stuff there instead actual subkeys
ok
YO IT DELETED
WAIT WHAT
HOW DID IT DELETE BUT THE OTHER DIDNT??
im confused
So that you don't have any issues, since registry is pretty important to mess with
I think
It might be permission issue
You opened VS as admin?
yea
Weird
Since there's no issue with code
Wait lemme try in HKLM
alr
I got it
My guy
Keep space between 7 - Zip
Do one thinf
alr
Go to reg key right click and copy the path
And paste it
done
ty ima test
You copied the path correctly right?
yea
Weird
I'm getting an issue too
Alright
Fixed
1 min
alr
Nah wasn't successful
same
Alr let's gooooooi
I did it
nice
Change the 7 - Zip to 7-zip
alr
done lemme test
Before that
This
Do this*
Wait imma paste some code
alr
var subKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(keyPath, true);
private void button20_Click_1(object sender, EventArgs e)
{
// const string registryKey = @"SOFTWARE\7-Zip";
// const string valueName = "what2";
try
{
// Open the registry key
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\7-zip", true))
{
// Check if the key exists
if (key != null)
{
// Delete the value
key.DeleteValue("what");
MessageBox.Show("Registry value deleted successfully.");
} else { MessageBox.Show("Registry key does not exist."); } } } catch (Exception ex) { MessageBox.Show("An error occurred: " + ex.Message); } } } } where do i put it here?
} else { MessageBox.Show("Registry key does not exist."); } } } catch (Exception ex) { MessageBox.Show("An error occurred: " + ex.Message); } } } } where do i put it here?
wait
Ye exactly
alr
ty
Place it inside the brackets
here?
No after using and inside brackets
Or or after key =
oh ok
Copy the subkey = part and paste it
this?
No no
Using (registry key = bla bla)
oh alr
Instead of bla bla you can copy the right side of subkey = and paste it
like this
Yes but do this
Add this Registry key = before registrykey.openbasekey
And replace keypath in the same line with the actual registry path
still errors or im doing it wrong
Yes you didn't put RegisteryKey key = before the RegisteryKey.OpenBaseKey in using (....)
oh ok
done, still errors, idk what im doing wrong
wait
i made it not error
Yes that's one thing
but these are wrong
But you still didn't add this
i thought i did
WAIT
I'm on my mobile else id have typed the code
ok wait i think ik what i did wrong
Ye you didn't
wait
im so confused rn
You're almost there
Just add = AFTER key
oh ok
done im still doing it wrong prob
Na na it's RegistryKey key
Not Registry key
oh alr
Or just make it var
Var key
i got it working but it still errors
No worries just run it
alr
ima try
wait
i know my error
Aight
wait did i do it right?
No no remove the semi colon at the end of using line
DONE
TY
no errors
Gg run it
YO IT WORKS
TYSM
Finally
YO tysm
Bro ur a legend
TY again
Gg mate!
Ty again
You're welcome 😼
im going to test it on an actual key
tysm again
hey i got another question
its very complicated
on my other windows form, when i click a button, the registry dword changes, but on another form, it doesnt change
how do i fix that
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.