C#实现文件与应用程序关联的方法

问题遇到的现象和发生背景

C#实现文件与应用程序关联的方法 使用的是修改注册表方法 在网上找了几个例程运行 报错访问注册表被拒绝

问题相关代码,请勿粘贴截图
    static void RegFileExt()
    {
        try
        {
            string boardExeFullName = Application.ExecutablePath;
            if (File.Exists(boardExeFullName))
            {
                string MyExtName = ".pjt";
                string MyType = "dbb_auto_file";
                string MyContent = "application/pjt";
                string command = "\"" + boardExeFullName + "\"" + " \"%1\"";
                RegistryKey key = Registry.ClassesRoot.OpenSubKey(MyType);
                if (key == null)
                {
                    RegistryKey MyReg = Registry.ClassesRoot.CreateSubKey(MyExtName);
                    MyReg.SetValue("", MyType);
                    MyReg.SetValue("Content Type", MyContent);
                    MyReg = Registry.ClassesRoot.CreateSubKey(MyType);
                    MyReg.SetValue("", MyType);
                    MyReg = MyReg.CreateSubKey("Shell\\Open\\Command");
                    MyReg.SetValue("", command);
                    MyReg.Close();
                }
                else
                {
                    var myReg = key.OpenSubKey("Shell\\Open\\Command", true);
                    if (myReg != null && (myReg.GetValue("") == null || myReg.GetValue("").ToString() != command))
                    {
                        myReg.SetValue("", command);//解决因目录变化导致 注册表失效的问题
                        myReg.Close();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
运行结果及报错内容

img

我的解答思路和尝试过的方法
我想要达到的结果

程序导出文件关联程序 打开文件可以打开程序并读取内容

用管理员权限打开程序

管理员身份运行