C
C#2y ago
2yxle

❔ 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
Shinigami
Shinigami2y ago
Where is the reg key located? And you mean button in visual studio? Or in forms or wpf?
2yxle
2yxleOP2y ago
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
Shinigami
Shinigami2y ago
You might need admin privileges to delete a key in HKLM
2yxle
2yxleOP2y ago
it has admin privileges but this is the script that doesnt work: wait a minute lemme find my script
Shinigami
Shinigami2y ago
Sure
2yxle
2yxleOP2y ago
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); } }
Shinigami
Shinigami2y ago
You're searching in the wrong hive hkcu Registry.CurrentUser
2yxle
2yxleOP2y ago
oh so i change it to localmachine?
Shinigami
Shinigami2y ago
Yes
2yxle
2yxleOP2y ago
TY ill tell u if its fixed or not
Shinigami
Shinigami2y ago
Yea LocalMachine Make sure L and M are capital
2yxle
2yxleOP2y ago
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
2yxle
2yxleOP2y ago
Shinigami
Shinigami2y ago
Gimme 10 mins
2yxle
2yxleOP2y ago
alr ty
Shinigami
Shinigami2y ago
Aight Soo what's happening is you're deleting the value of what Not the subkey itself Soo do this instead
2yxle
2yxleOP2y ago
oh
Shinigami
Shinigami2y ago
Key.DeleteSubKey(valuename);
2yxle
2yxleOP2y ago
wait where do i put it
2yxle
2yxleOP2y ago
2yxle
2yxleOP2y ago
it has an error wait nvm i fixed it ty again ima test it
Shinigami
Shinigami2y ago
valueName Yea go ahead
2yxle
2yxleOP2y ago
Shinigami
Shinigami2y ago
Can you show me the whole tree? I mean the registry tree
2yxle
2yxleOP2y ago
ok
2yxle
2yxleOP2y ago
2yxle
2yxleOP2y ago
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
Shinigami
Shinigami2y ago
Alright, 1 min
2yxle
2yxleOP2y ago
ty
Shinigami
Shinigami2y ago
Ideally this should work Do one thing instead of deleting Get the value of the subkey Object value = Key.GetValue(valueName); MessageBox.Show(value);
2yxle
2yxleOP2y ago
right here right?
Shinigami
Shinigami2y ago
Yup Just comment them out
2yxle
2yxleOP2y ago
alr ty
2yxle
2yxleOP2y ago
Shinigami
Shinigami2y ago
Instead of object make it var value
2yxle
2yxleOP2y ago
alr it still has the error
Shinigami
Shinigami2y ago
Errr give me 5 mins imma test it on my machine
2yxle
2yxleOP2y ago
alr ty
Shinigami
Shinigami2y ago
Alright just tested it out
2yxle
2yxleOP2y ago
alr
Shinigami
Shinigami2y ago
It's key.DeleteValue();
2yxle
2yxleOP2y ago
where do i put it
2yxle
2yxleOP2y ago
Shinigami
Shinigami2y ago
Remove these 2 lines And replace key.deletesubkey with deletekey My bad you were doing the right thing all along
2yxle
2yxleOP2y ago
is it this?
Shinigami
Shinigami2y ago
Exactly I don't see any reason why this won't work
2yxle
2yxleOP2y ago
alr ima test it again
2yxle
2yxleOP2y ago
thats really weird
2yxle
2yxleOP2y ago
Shinigami
Shinigami2y ago
Do one thing Instead of variables just keep the paths
2yxle
2yxleOP2y ago
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); } } } }
Shinigami
Shinigami2y ago
Instead of opensubkey(variable) Just keep that path there Opensubkey (the path)
2yxle
2yxleOP2y ago
alr ty
2yxle
2yxleOP2y ago
this right?
Shinigami
Shinigami2y ago
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
2yxle
2yxleOP2y ago
alr ima test it
Shinigami
Shinigami2y ago
Sure
2yxle
2yxleOP2y ago
2yxle
2yxleOP2y ago
2yxle
2yxleOP2y ago
Shinigami
Shinigami2y ago
Do one thing Create a key in HKCU hive And change the registry.LocalMachine to registry.CurrentUser
2yxle
2yxleOP2y ago
ok
2yxle
2yxleOP2y ago
2yxle
2yxleOP2y ago
i will create it here wait nvm
2yxle
2yxleOP2y ago
2yxle
2yxleOP2y ago
here i will create it here
Shinigami
Shinigami2y ago
Sure
2yxle
2yxleOP2y ago
Shinigami
Shinigami2y ago
I'd suggest you create a dummy subkey in HKCU and try all sorts of stuff there instead actual subkeys
2yxle
2yxleOP2y ago
ok YO IT DELETED WAIT WHAT HOW DID IT DELETE BUT THE OTHER DIDNT?? im confused
Shinigami
Shinigami2y ago
So that you don't have any issues, since registry is pretty important to mess with I think
2yxle
2yxleOP2y ago
Shinigami
Shinigami2y ago
It might be permission issue You opened VS as admin?
2yxle
2yxleOP2y ago
yea
Shinigami
Shinigami2y ago
Weird Since there's no issue with code Wait lemme try in HKLM
2yxle
2yxleOP2y ago
alr
Shinigami
Shinigami2y ago
I got it My guy Keep space between 7 - Zip Do one thinf
2yxle
2yxleOP2y ago
alr
Shinigami
Shinigami2y ago
Go to reg key right click and copy the path And paste it
2yxle
2yxleOP2y ago
done ty ima test
2yxle
2yxleOP2y ago
2yxle
2yxleOP2y ago
Shinigami
Shinigami2y ago
You copied the path correctly right?
2yxle
2yxleOP2y ago
yea
2yxle
2yxleOP2y ago
Shinigami
Shinigami2y ago
Weird I'm getting an issue too Alright Fixed 1 min
2yxle
2yxleOP2y ago
alr
Shinigami
Shinigami2y ago
Nah wasn't successful
2yxle
2yxleOP2y ago
same
Shinigami
Shinigami2y ago
Alr let's gooooooi I did it
2yxle
2yxleOP2y ago
nice
Shinigami
Shinigami2y ago
Change the 7 - Zip to 7-zip
2yxle
2yxleOP2y ago
alr done lemme test
Shinigami
Shinigami2y ago
Before that This Do this* Wait imma paste some code
2yxle
2yxleOP2y ago
alr
Shinigami
Shinigami2y ago
var subKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64).OpenSubKey(keyPath, true);
2yxle
2yxleOP2y ago
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?
2yxle
2yxleOP2y ago
2yxle
2yxleOP2y ago
wait
Shinigami
Shinigami2y ago
Ye exactly
2yxle
2yxleOP2y ago
alr ty
Shinigami
Shinigami2y ago
Place it inside the brackets
2yxle
2yxleOP2y ago
here?
Shinigami
Shinigami2y ago
No after using and inside brackets Or or after key =
2yxle
2yxleOP2y ago
oh ok
Shinigami
Shinigami2y ago
Copy the subkey = part and paste it
2yxle
2yxleOP2y ago
this?
Shinigami
Shinigami2y ago
No no Using (registry key = bla bla)
2yxle
2yxleOP2y ago
oh alr
Shinigami
Shinigami2y ago
Instead of bla bla you can copy the right side of subkey = and paste it
2yxle
2yxleOP2y ago
like this
2yxle
2yxleOP2y ago
Shinigami
Shinigami2y ago
Yes but do this Add this Registry key = before registrykey.openbasekey And replace keypath in the same line with the actual registry path
2yxle
2yxleOP2y ago
still errors or im doing it wrong
Shinigami
Shinigami2y ago
Yes you didn't put RegisteryKey key = before the RegisteryKey.OpenBaseKey in using (....)
2yxle
2yxleOP2y ago
oh ok
2yxle
2yxleOP2y ago
done, still errors, idk what im doing wrong
2yxle
2yxleOP2y ago
wait
2yxle
2yxleOP2y ago
2yxle
2yxleOP2y ago
i made it not error
Shinigami
Shinigami2y ago
Yes that's one thing
2yxle
2yxleOP2y ago
but these are wrong
Shinigami
Shinigami2y ago
But you still didn't add this
2yxle
2yxleOP2y ago
i thought i did
2yxle
2yxleOP2y ago
WAIT
Shinigami
Shinigami2y ago
I'm on my mobile else id have typed the code
2yxle
2yxleOP2y ago
ok wait i think ik what i did wrong
Shinigami
Shinigami2y ago
Ye you didn't
2yxle
2yxleOP2y ago
wait
2yxle
2yxleOP2y ago
im so confused rn
Shinigami
Shinigami2y ago
You're almost there Just add = AFTER key
2yxle
2yxleOP2y ago
oh ok
2yxle
2yxleOP2y ago
done im still doing it wrong prob
Shinigami
Shinigami2y ago
Na na it's RegistryKey key Not Registry key
2yxle
2yxleOP2y ago
oh alr
Shinigami
Shinigami2y ago
Or just make it var Var key
2yxle
2yxleOP2y ago
i got it working but it still errors
Shinigami
Shinigami2y ago
No worries just run it
2yxle
2yxleOP2y ago
alr ima try
2yxle
2yxleOP2y ago
2yxle
2yxleOP2y ago
2yxle
2yxleOP2y ago
wait i know my error
Shinigami
Shinigami2y ago
Aight
2yxle
2yxleOP2y ago
wait did i do it right?
Shinigami
Shinigami2y ago
No no remove the semi colon at the end of using line
2yxle
2yxleOP2y ago
DONE TY no errors
Shinigami
Shinigami2y ago
Gg run it
2yxle
2yxleOP2y ago
YO IT WORKS TYSM
Shinigami
Shinigami2y ago
Finally
2yxle
2yxleOP2y ago
YO tysm Bro ur a legend TY again
Shinigami
Shinigami2y ago
Gg mate!
2yxle
2yxleOP2y ago
Ty again
Shinigami
Shinigami2y ago
You're welcome 😼
2yxle
2yxleOP2y ago
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
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server