Search code examples
c#visual-studioregeditregistrykey

WPF application doesn't work with its own registry key


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();
    }

Solution

  • 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