/// <summary> /// 更新指定文件类型关联信息 /// /// 错误处理:无 /// /// </summary> public static bool UpdateFileTypeRegInfo(I3FileTypeRegInfo regInfo) { if (!FileTypeRegistered(regInfo.ExtendName)) { return(false); } string extendName = regInfo.ExtendName; RegistryKey extendKey = Registry.ClassesRoot.OpenSubKey(extendName); string relationName = extendKey.GetValue("").ToString(); 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> public static void RegisterFileType(I3FileTypeRegInfo regInfo) { if (FileTypeRegistered(regInfo.ExtendName)) { return; } //xcf_FileType string relationName = I3StringUtil.SubString(regInfo.ExtendName, 1, regInfo.ExtendName.Length - 1).ToUpper() + "_FileType"; //指定.xcf文件的关联信息在 xcf_FileType中 RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName); //创建项.xcf fileTypeKey.SetValue("", relationName); //在.xcf 中增加一个默认值为 xcf_FileType fileTypeKey.Close(); RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName); //创建项xcf_FileType 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(); }
/// <summary> /// 获取指定文件类型关联信息 /// 类似于 .xlsm ,大小写都可以 /// 错误处理:无 /// 注意:返回的ExePath是两头带"的,用File.Exists来判断时会找不到 /// </summary> public static I3FileTypeRegInfo GetFileTypeRegInfo(string extendName) { if (!FileTypeRegistered(extendName)) { return(null); } I3FileTypeRegInfo regInfo = new I3FileTypeRegInfo(extendName); RegistryKey extendKey = Registry.ClassesRoot.OpenSubKey(extendName); string relationName = extendKey.GetValue("").ToString(); 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 = I3StringUtil.SubString(temp, 0, temp.Length - 3); return(regInfo); }