/// <summary> /// GetFileTypeRegInfo 得到指定文件类型关联信息 /// </summary> public static FileTypeRegInfo GetRegInfo(string extendName) { if (!CheckIfRegistered(extendName)) { return null; } FileTypeRegInfo regInfo = new FileTypeRegInfo(extendName); try { 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; } catch { return null; } }
/// <summary> /// RegisterFileType 使文件类型与对应的图标及应用程序关联起来。 /// </summary> public static bool Register(FileTypeRegInfo regInfo) { if (CheckIfRegistered(regInfo.ExtendName)) { return(true); } try { 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(); return(true); } catch { return(false); } }
/// <summary> /// GetFileTypeRegInfo 得到指定文件类型关联信息 /// </summary> public static FileTypeRegInfo GetRegInfo(string extendName) { if (!CheckIfRegistered(extendName)) { return(null); } FileTypeRegInfo regInfo = new FileTypeRegInfo(extendName); try { 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); } catch { return(null); } }
private void regExtFile() { Assembly asm = Assembly.GetExecutingAssembly(); string resourceName = "MEditor.logo.ico"; Stream stream = asm.GetManifestResourceStream(resourceName); string icofile = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\markdown.ico"; FileStream fs = new FileStream(icofile, FileMode.Create, FileAccess.Write); byte[] bs = new byte[stream.Length]; stream.Read(bs, 0, (int)stream.Length); fs.Write(bs, 0, bs.Length); fs.Flush(); fs.Close(); fs.Dispose(); //注册关联的文件扩展名 FileTypeRegInfo fr = new FileTypeRegInfo(); fr.Description = "markdown文档格式"; fr.ExePath = Application.ExecutablePath; fr.ExtendName = _myExtName; fr.IcoPath = icofile; bool rel = true; if (FileTypeRegister.CheckIfRegistered(fr.ExtendName)) { rel = FileTypeRegister.UpdateRegInfo(fr); } else { rel = FileTypeRegister.Register(fr); } if (!rel && IsWindowsVistaOrNewer) { MessageBox.Show("您是在windows7 下运行的此程序,如果执行完扩展名关联但没有成功,请您以“管理员权限方式再打开此程序再关联一次”!"); } }
private void regExtFile() { Assembly asm = Assembly.GetExecutingAssembly(); string resourceName = "MEditor.logo.ico"; Stream stream = asm.GetManifestResourceStream(resourceName); string icofile = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\markdown.ico"; var fs = new FileStream(icofile, FileMode.Create, FileAccess.Write); var bs = new byte[stream.Length]; stream.Read(bs, 0, (int)stream.Length); fs.Write(bs, 0, bs.Length); fs.Flush(); fs.Close(); fs.Dispose(); //注册关联的文件扩展名 var fr = new FileTypeRegInfo(); fr.Description = "markdown文档格式"; fr.ExePath = Application.ExecutablePath; fr.ExtendName = _myExtName; fr.IcoPath = icofile; bool rel = true; if (FileTypeRegister.CheckIfRegistered(fr.ExtendName)) { rel = FileTypeRegister.UpdateRegInfo(fr); } else { rel = FileTypeRegister.Register(fr); } if (!rel && IsWindowsVistaOrNewer) { MessageBox.Show("您是在windows7 下运行的此程序,如果执行完扩展名关联但没有成功,请您以“管理员权限方式再打开此程序再关联一次”!"); } }
/// <summary> /// UpdateFileTypeRegInfo 更新指定文件类型关联信息 /// </summary> public static bool UpdateRegInfo(FileTypeRegInfo regInfo) { if (!CheckIfRegistered(regInfo.ExtendName)) { return false; } try { 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; } catch { return false; } }