一、关联代码

        /// <summary>
        /// 关联程序和类型
        /// </summary>
        private void RegFileExt()
        {
            try
            {
                string boardExe = @"BlackBoardTool\bin\Teacher.UCBook.BlackBord.exe";
                string boardExeFullName = Path.Combine(GlobalInfos.ExePath, boardExe);
                if (File.Exists(boardExeFullName))
                {
                    string MyExtName = ".dbb";
                    string MyType = "dbb_auto_file";
                    string MyContent = "application/dbb";
                    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();
                        _logger.Info("设置文件关联操作成功!");
                    }
                    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)
            {
                _logger.ErrorFormat("设置文件关联失败!{0}",ex.Message);
            }
        }
        
View Code

相关文章:

  • 2021-08-14
  • 2021-10-01
  • 2022-01-16
  • 2022-12-23
  • 2022-12-23
  • 2021-12-20
  • 2022-12-23
  • 2021-11-24
猜你喜欢
  • 2021-08-27
  • 2021-08-06
  • 2021-11-17
  • 2021-11-22
  • 2022-01-08
  • 2021-12-21
  • 2021-08-16
相关资源
相似解决方案