/// <summary> /// RegisterFileType 使文件类型与对应的图标及应用程序关联起来。 /// </summary> public static void RegisterFileType(FileTypeRegInfo regInfo) { try { if (FileTypeRegister.FileTypeRegistered(regInfo.ExtendName)) { UpdateFileTypeRegInfo(regInfo); } string relationName = regInfo.ExtendName.Substring(1, regInfo.ExtendName.Length - 1).ToUpper() + "_FileType"; RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName); fileTypeKey.SetValue("", relationName); fileTypeKey.Close(); RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName); relationKey.SetValue("", regInfo.Description); RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon"); iconKey.SetValue("", regInfo.IcoPath); RegistryKey shellKey = relationKey.CreateSubKey("Shell"); RegistryKey openKey = shellKey.CreateSubKey("Open"); RegistryKey commandKey = openKey.CreateSubKey("Command"); commandKey.SetValue("", "\"" + regInfo.ExePath + "\"%1\" %*"); relationKey.Close(); } catch (Exception ex) { Console.WriteLine("Filetype Register:" + ex.ToString()); } }
/// <summary> /// GetFileTypeRegInfo 得到指定文件类型关联信息 /// </summary> public static FileTypeRegInfo GetFileTypeRegInfo(string extendName) { if (!FileTypeRegister.FileTypeRegistered(extendName)) { return(null); } FileTypeRegInfo regInfo = new FileTypeRegInfo(extendName); string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType"; RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName); regInfo.Description = relationKey.GetValue("").ToString(); RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon"); regInfo.IcoPath = iconKey.GetValue("").ToString(); RegistryKey shellKey = relationKey.OpenSubKey("Shell"); RegistryKey openKey = shellKey.OpenSubKey("Open"); RegistryKey commandKey = openKey.OpenSubKey("Command"); string temp = commandKey.GetValue("").ToString(); regInfo.ExePath = temp.Substring(0, temp.Length - 3); return(regInfo); }
/// <summary> /// UpdateFileTypeRegInfo 更新指定文件类型关联信息 /// </summary> public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo) { if (!FileTypeRegister.FileTypeRegistered(regInfo.ExtendName)) { return(false); } string extendName = regInfo.ExtendName; string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType"; RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName, true); relationKey.SetValue("", regInfo.Description); RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon", true); iconKey.SetValue("", regInfo.IcoPath); RegistryKey shellKey = relationKey.OpenSubKey("Shell"); RegistryKey openKey = shellKey.OpenSubKey("Open"); RegistryKey commandKey = openKey.OpenSubKey("Command", true); commandKey.SetValue("", regInfo.ExePath + " %1"); relationKey.Close(); return(true); }
/// <summary> /// 注册文件关联_调试时使用 /// </summary> /// <param name="exePath">程序</param> /// <param name="ResoulePath">关联图标库文件</param> /// <param name="fileTypes">要关联的文件类型集合</param> public static void Regsiter(string exePath, string ResoulePath, List <string> fileTypes) { FileTypeRegInfo fti = null; FileTip ft = null; foreach (string type in fileTypes) { ft = GetIcoReflect(type); string icoPath = ResoulePath + "," + ft.IcoIndex; fti = new FileTypeRegInfo { ExePath = exePath, ExtendName = type, IcoPath = icoPath, Description = "Cup Player:" + ft.TypeDescription }; FileTypeRegister.RegisterFileType(fti); } FileTypeRegister.RegisterFileType(new FileTypeRegInfo { ExePath = exePath, ExtendName = ".pldb", IcoPath = AppDomain.CurrentDomain.BaseDirectory + "/logo.ico", Description = "Cup Player:播放列表文件" }); SHChangeNotify(HChangeNotifyEventID.SHCNE_ASSOCCHANGED, HChangeNotifyFlags.SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero); }