C#自动注册自定义文件类型
2010-11-09 16:39
501 查看
(网上抄袭而来 如有侵权请提醒~)
关键代码我通过FileTypeRegister静态类来完成这些功能。首先,将注册需要用到的信息封装成FileTypeRegInfo,定义如下: public class FileTypeRegInfo { /// <summary> /// 目标类型文件的扩展名 /// </summary> public string ExtendName ; //".xcf" /// <summary> /// 目标文件类型说明 /// </summary> public string Description ; //"XCodeFactory项目文件" /// <summary> /// 目标类型文件关联的图标 /// </summary> public string IcoPath ; /// <summary> /// 打开目标类型文件的应用程序 /// </summary> public string ExePath ; public FileTypeRegInfo() { } public FileTypeRegInfo(string extendName) { this.ExtendName = extendName ; } } FileTypeRegister类主要是操作注册表中的内容,实现如下: /// <summary> /// FileTypeRegister 用于注册自定义的文件类型。 /// zhuweisky 2005.08.31 /// </summary> public class FileTypeRegister { RegisterFileType#region RegisterFileType /// <summary> /// RegisterFileType 使文件类型与对应的图标及应用程序关联起来。 /// </summary> public static void RegisterFileType(FileTypeRegInfo regInfo) { if(RegistryHelper.FileTypeRegistered(regInfo.ExtendName)) { return ; } 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() ; } /// <summary> /// GetFileTypeRegInfo 得到指定文件类型关联信息 /// </summary> public static FileTypeRegInfo GetFileTypeRegInfo(string extendName) { if(! RegistryHelper.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(! RegistryHelper.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> /// FileTypeRegistered 指定文件类型是否已经注册 /// </summary> public static bool FileTypeRegistered(string extendName) { RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName); if(softwareKey != null) { return true ; } return false ; } #endregion } 要注意的是commandKey.SetValue("" ,regInfo.ExePath + " %1") ;其中" %1"表示将被双击的文件的路径传给目标应用程序,这样在双击a.xcf文件时,XCodeFactory才知道要打开哪个文件,所以应用程序的Main方法要被改写为带有参数的形式,就像下面的样子: [STAThread] static void Main(string[] args) { if((args!= null) && (args.Length > 0)) { string filePath = "" ; for(int i=0 ;i<args.Length ;i++) { filePath += " " + args[i] ; } MainForm.XcfFilePath = filePath.Trim() ; } Application.Run(new MainForm()); } 关于自定义文件类型的注册,本文实现的是最基本的功能,如果需要更多的高级功能,也可以类推实现之。 工作空间: 在一个应用程序中只允许加载一个SuperWorkspace控件,称为工作空间,存放一些如数据名称、数量、相对路径、打开方式等简单的描述信息,是一个范围定义
相关文章推荐
- C# 自动注册自定义文件类型
- 使用C#自动注册自定义文件类型
- C# 自动注册自定义文件类型
- C#实现自动注册自定义文件类型介绍
- 使用C#自动注册自定义文件类型
- C# 注册自定义文件类型 实现自定义文件类型关联默认应用程序
- 自动注册自定义文件类型
- 自动注册自定义文件类型
- 动态ActionForm、struts上传文件、自定义ActionForm的类型转换、Struts对ActionForm的自动搜集流程
- 使用C#自动注册自定义文件类型
- 使用C#自动注册自定义文件类型
- C#项目的.settings文件中的自定义类型(及其数组)的配置步骤
- C#中读取“已注册的文件类型”的图标及读取指定文件图标的方法
- C#中读取“已注册的文件类型”的图标及读取指定文件图标的方法
- C#中读取“已注册的文件类型”的图标及读取指定文件图标的方法
- Q_DECLARE_METATYPE(继承QObject的类都已经自动注册),注册后的类型可以作为QVariant的自定义类型
- C#遍历目录中所有自定义类型文件
- C#中读取“已注册的文件类型”的图标及读取指定文件图标的方法
- C#中读取“已注册的文件类型”的图标及读取指定文件图标的方法
- C#中读取“已注册的文件类型”的图标及读取指定文件图标的方法 (转)