/// <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); } }
时间: 2024-10-15 04:02:12