HeyMyBoy
HeyMyBoy

Reputation: 3

How do I delete a registry key?

This is my code:

Dim exePath As String = Application.ExecutablePath()
Dim key As Microsoft.Win32.RegistryKey
key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Compatibility Assistant\Store", True)
key.DeleteValue(exePath)

But I don't know why the application doesn't delete that key.

Example: It finds the key but it doesn't delete that key

enter image description here

Upvotes: 0

Views: 2772

Answers (2)

Visual Vincent
Visual Vincent

Reputation: 18320

That value is created automatically by the system when starting the application, meaning you cannot remove it while your app is running.

You can delete it after your app has closed if you want, but you must delete it from a separate application and it will be re-created again the next time you launch your app.

Starting a simple cmd instance to do the deleting for you is enough:

Dim psi As New ProcessStartInfo("cmd.exe", "/C timeout /t 3 /nobreak > NUL & reg delete ""HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Compatibility Assistant\Store"" /v """ & Application.ExecutablePath & """ /f")
psi.CreateNoWindow = True
psi.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(psi)

Upvotes: 0

Aousaf Rashid
Aousaf Rashid

Reputation: 5758

In order to delete the key,use the DeleteSubKey method. An example would look like this:

My.computer.Registry.Currentuser.DeleteSubKey(keypath)

However, according to MSDN, exceptions may occur if

  1. The name of the key is nothing or null

  2. The user doesn't have required permission to delete/read/write the key

  3. The key name exceeds 255 characters

  4. The key is set to read-only

Upvotes: 2

Related Questions