建立数据文件与程序的关联

实现效果:

  

知识运用:

  public void DeleteSubKey(string subkey)    //删除指定的子项

实现代码:

        public void RefreshSystem()
        {
            Process[] ps = Process.GetProcessesByName("explorer");
            foreach (Process p in ps)
            {
                p.Kill();  
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string notePath = "C:\Windows\System32\notepad.exe";
                string extFile = ".ffy";
                string myContent = "text/plain";
                RegistryKey rk = Registry.ClassesRoot;
                rk = rk.CreateSubKey(extFile);
                rk.SetValue("","自定义的拓展名");
                rk.SetValue("Content Type",myContent);
                rk=rk.CreateSubKey("shell\open\Command");
                rk.SetValue("",notePath+" %1");
                rk.Close();
                if (MessageBox.Show("关联成功!") == DialogResult.OK)
                {
                    RefreshSystem(); //刷新explorer进程
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string extFile = ".ffy";
            RegistryKey rk = Registry.ClassesRoot;
            rk.CreateSubKey(extFile + "\shell\open\Command");
            rk.DeleteSubKey(extFile+"\shell\open\Command");
            if (MessageBox.Show("关联已取消!") == DialogResult.OK)
            {
                RefreshSystem(); //刷新explorer进程
            }
        }

  

原文地址:https://www.cnblogs.com/feiyucha/p/10327558.html