简体   繁体   English

WPF 应用程序无法使用自己的注册表项

[英]WPF application doesn't work with its own registry key

I'm trying to add the classic "Send with MyApp" in the ContextMenu.我正在尝试在 ContextMenu 中添加经典的“使用 MyApp 发送”。

The fact is that my program modifies the windows registry, but it seems that it can't see the update version of it.事实是我的程序修改了windows注册表,但是好像看不到它的更新版本。 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)?我该如何解决这个问题(不创建另一个修改 windows 注册表然后调用我的程序)?

Thank you in advance for the help.预先感谢您的帮助。

Ps Here are the functions that I use to modify the registry ps这里是我用来修改注册表的函数

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

Have you tried to read this?你试过读这个吗? Edited the registry with C# but cannot find the change with regedit 使用 C# 编辑了注册表,但无法使用 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 -->我几年前发现了这个问题,我认为必须使用(至少)两个不同的 C# 线程来查看注册表项的更改 -->

ref: C#: How to change windows registry and take effect immediately ref: C#: 如何更改 windows 注册表并立即生效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM