1

I'm trying to add the classic "Send with MyApp" in the ContextMenu.

The fact is that my program modifies the windows registry, but it seems that it can't see the update version of it. Indeed, if I start again my program leaving the keys that it modified, it works fine.

How can I solve this (without create another program that modfies the windows registry and then call mine)?

Thank you in advance for the help.

P.s. Here are the functions that I use to modify the registry

private void AddOption_ContextMenu()
    {
        RegistryKey _key1 = Registry.ClassesRoot.OpenSubKey("Folder\\shell", true);
        RegistryKey _key = Registry.ClassesRoot.OpenSubKey("*\\shell", true);

        RegistryKey newkey = _key.CreateSubKey("MyApp");
        RegistryKey newkey1 = _key1.CreateSubKey("MyApp");
        RegistryKey command = newkey.CreateSubKey("command");
        RegistryKey command1 = newkey1.CreateSubKey("command");
        string program = Path.GetDirectoryName(Application.ResourceAssembly.Location);

        for (int i = 0; i < 3; i++)
            program = Path.GetDirectoryName(program);

        program = @"""" + program + @"\\MyApp\\bin\\Debug\\MyApp.exe"" ""%1""";
        command.SetValue("", program);
        command1.SetValue("", program);
        newkey.SetValue("", "Send with MyApp");
        newkey.SetValue("Icon", Path.GetDirectoryName(Application.ResourceAssembly.Location) + "\\icon.ico");
        newkey1.SetValue("", "Send with MyApp");
        newkey1.SetValue("Icon", Path.GetDirectoryName(Application.ResourceAssembly.Location) + "\\icon.ico");

        command.Close();
        command1.Close();

        newkey1.Close();
        newkey.Close();
        _key.Close();
    }
    public void RemoveOption_ContextMenu()
    {
        RegistryKey _key = Registry.ClassesRoot.OpenSubKey("*\\shell", true);
        RegistryKey _key1 = Registry.ClassesRoot.OpenSubKey("Folder\\shell", true);
        _key.DeleteSubKeyTree("MyApp");
        _key1.DeleteSubKeyTree("MyApp");
        _key1.Close();
        _key.Close();
    }
  • What do you mean by "it can't see the update version of it"? Where do you read the created registry keys? – Klaus Gütter Aug 10 '20 at 10:08
  • BTW: you forgot to close _key1 in the first method. I recommend to use `using (RegistryKey key = ...)` instead. – Klaus Gütter Aug 10 '20 at 10:15
  • If I open "regedit" after the application started, I can see the modified keys and they works but only if I close the program and re-run it without modify anything in the registry. I'm sorry if it sounds strange, but I don't know how to explain it differently. – Luca Vezzani Aug 10 '20 at 10:22
  • 1
    I still don't get what you mean by "it can't see the update version of it". In the code you provided you only modify the registry but do not read it. So what does "see" mean? – Klaus Gütter Aug 10 '20 at 10:42
  • The program modifies the registry. Then, when it's running and I click on the contextMenu of a file, I can see the "Send with MyApp" option but it doesn't work. The "it can't see the update version of it" is a personal opinion based on the fact that if I close and restart the program (this time without modifying anything in the registry) it works fine. – Luca Vezzani Aug 10 '20 at 11:09
  • So you click on the context menu of the file in Windows Explorer and not in your app? – Klaus Gütter Aug 10 '20 at 11:42

1 Answers1

1

Have you tried to read this? Edited the registry with C# but cannot find the change with regedit

I found this issue years ago and I think that is mandatory to use (at least) two different C# threads to see changes in registry key -->

ref: C# : How to change windows registry and take effect immediately

GCiandro
  • 66
  • 9
  • No, writing to the registry has immediate effects. Only when your app or a component you uses caches registry values, these require refreshing. – Klaus Gütter Aug 10 '20 at 11:00
  • 1
    Reading the question I thought that Luca wants to read reg keys modified in debug, that does not work by the same thread (that cached starting conditions). – GCiandro Aug 10 '20 at 11:07
  • 1
    Thank you GCiandro, I had the opportunity to test it and, with a dedicated thread for the changes in the registry, it works perfectly. – Luca Vezzani Aug 10 '20 at 12:42