Search code examples
vb.netregeditregistrykey

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


Solution

  • 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)