0

Hey guys so I'm trying to delete the Run history when the user presses Windows + R key so there is no history I made a method for this, and you will see the parameter this is HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU

But I'm worried I will delete the wrong items, this is my code:

 //this method will clear the run history from the registry
    private void ClearRunHistory(string RegRunHistoryDirectory)
    {
        using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RegRunHistoryDirectory, true))
        {
            if (key == null)
            {
                //if key doesnt exist then do nothing
            }

            else
            {
                key.DeleteValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU");
            }
        }

    }
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
  • 3
    In what sense are you worried you will delete the wrong registry item? Are you asking "Is this the registry item that stores the run history?", or are you asking "Is there any possibility my code might delete a different registry item"? – RB. Apr 20 '16 at 08:55

3 Answers3

1

In the delete value you have to put only the value, not the entire path. For example if you want to delete id in the testsettings/id , in the key.DeleteValue you need to put only "id". The same logic applies here. What do you want to delete? A value or all the values under RegRunHistoryDirectory?

Kristi Jorgji
  • 1,154
  • 1
  • 14
  • 39
0

If you want to delete the run history, you want to delete every value of the registry key @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU" minus the (default) one.

(Luckily, the framework ignore the default one when you enumerate it)

On your code, instead, you are passing the entire key path to the key.DeleteValue() which is wrong.

A right approach is to first enumerate all the values of the key and then delete, here is an example:

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU", true))
{
     string[] values = key.GetValueNames();

     foreach (var item in values)
     {
         key.DeleteValue(item);
     }
}

Reference: https://support.microsoft.com/en-us/kb/142298

fruggiero
  • 943
  • 12
  • 20
  • Ok, if you are satisfied mark that as accepted answer when you have tried. (I've tried on my pc and is working) – fruggiero Apr 20 '16 at 09:32
  • Did it but I got a unauthorized exception. –  Apr 20 '16 at 09:38
  • You are logged as Administrator? – fruggiero Apr 20 '16 at 09:45
  • yeah I did, Maybe run my program as admin? –  Apr 20 '16 at 09:51
  • Yes try to run as admin, if it is working you can add an "application.manifest" on your visual studio solution so the application will prompt automatically the user to execute with elevated privileges. See here : http://stackoverflow.com/questions/2818179/how-to-force-my-net-application-to-run-as-administrator-on-windows-7 – fruggiero Apr 20 '16 at 09:53
  • On what line the exception did happen? – fruggiero Apr 20 '16 at 10:15
  • Are you sure that you writed **true** as second parameter to the OpenSubKey method? Another thing i would do is to check on **regedit** if there are subkeys of the **RunMRU** *Unauthorized Exception* can be throw if there are probems with permissions or if the key that you are deleting have subkeys – fruggiero Apr 20 '16 at 12:26
  • omg true wasn't there works perfect!! Sorry about that! It works! But what does the 'true' do? –  Apr 20 '16 at 22:57
  • True means to open the registry key with write access – fruggiero Apr 21 '16 at 09:37
  • Ah thank you, is there any documentation so I can get more in depth on this? If not its all good. Thanks again. –  Apr 22 '16 at 03:38
0

Hey guys just wanted to say that I fixed this :)

With the key.DeleteValue()

  • How's this different from the answer already provided by @fruggiero? Also you have already mentioned this in [comments](http://stackoverflow.com/a/36738928/5060335) – UditS Jun 08 '16 at 09:31