当前位置: 代码迷 >> C# >> C# 登记自定义文件类型 实现自定义文件类型关联应用程序
  详细解决方案

C# 登记自定义文件类型 实现自定义文件类型关联应用程序

热度:99   发布时间:2016-05-05 04:20:12.0
C# 注册自定义文件类型 实现自定义文件类型关联应用程序

    在我们自己编写的应用中,经常会用自定义类型的文件来保存与应用相关的数据,比如.osf文件就是应用程序的项目文件。如果没有向Windows注册表注册该文件类型,那么.osf文件的图标将是windows的文件默认图标,并且你双击一个a.osf文件,也不会自动启动应用程序来加载a.osf文件。如何使.osf文件的图标变成我自己喜爱的图标、如何完成像点击.doc文件就自动打开word 程序的功能,下面将告诉你解决方案。 


    我们可以通过手动修改注册表来完成上述任务,更好的方式是,通过程序来实现。这样在安装应用程序时,就可以自动的注册自定义文件类型了。我通过FileTypeRegister静态类来完成这些功能。首先,将注册需要用到的信息封装成FileTypeRegInfo,定义如下:

/// <summary>/// 文件类型注册信息/// </summary>public class FileTypeRegInfo{    /// <summary>      /// 扩展名      /// </summary>      public string ExtendName;  //".osf"      /// <summary>      /// 说明      /// </summary>      public string Description; //"OpenSelfFile项目文件"      /// <summary>      /// 关联的图标      /// </summary>      public string IconPath;    /// <summary>      /// 应用程序      /// </summary>      public string ExePath;    public FileTypeRegInfo()    {    }    public FileTypeRegInfo(string extendName)    {        this.ExtendName = extendName;    }}


FileTypeRegister类主要是操作注册表中的内容,实现如下:

/// <summary>  /// 注册自定义的文件类型。  /// </summary>  public class FileTypeRegister{    /// <summary>      /// 使文件类型与对应的图标及应用程序关联起来    /// </summary>              public static void RegisterFileType(FileTypeRegInfo regInfo)    {        if (FileTypeRegistered(regInfo.ExtendName))        {            return;        }                //HKEY_CLASSES_ROOT/.osf        RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName);        string relationName = regInfo.ExtendName.Substring(1,                                                            regInfo.ExtendName.Length - 1).ToUpper() + "_FileType";        fileTypeKey.SetValue("", relationName);        fileTypeKey.Close();        //HKEY_CLASSES_ROOT/OSF_FileType        RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName);        relationKey.SetValue("", regInfo.Description);        //HKEY_CLASSES_ROOT/OSF_FileType/Shell/DefaultIcon        RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon");        iconKey.SetValue("", regInfo.IconPath);        //HKEY_CLASSES_ROOT/OSF_FileType/Shell        RegistryKey shellKey = relationKey.CreateSubKey("Shell");        //HKEY_CLASSES_ROOT/OSF_FileType/Shell/Open        RegistryKey openKey = shellKey.CreateSubKey("Open");        //HKEY_CLASSES_ROOT/OSF_FileType/Shell/Open/Command        RegistryKey commandKey = openKey.CreateSubKey("Command");        commandKey.SetValue("", regInfo.ExePath + " %1"); // " %1"表示将被双击的文件的路径传给目标应用程序        relationKey.Close();    }    /// <summary>      /// 更新指定文件类型关联信息      /// </summary>          public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo)    {        if (!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.IconPath);        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 FileTypeRegInfo GetFileTypeRegInfo(string extendName)    {        if (!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.IconPath = 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>      /// 指定文件类型是否已经注册      /// </summary>              public static bool FileTypeRegistered(string extendName)    {        RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName);        if (softwareKey != null)        {            return true;        }        return false;    }}  

应用程序的Main方法要被改写为带有参数的形式,就像下面的样子:
using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using System.Windows.Forms;namespace OpenSelfFile{    static class Program    {        /// <summary>        /// 应用程序的主入口点。 -- 加上string[] args参数        /// </summary>        [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]; // 1个参数                }                MainWnd._osfFilePath = filePath.Trim();            }              Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            Application.Run(new MainWnd());        }    }}

在Form_load事件中进行注册:

private void Form1_Load(object sender, EventArgs e){    if (!FileTypeRegister.FileTypeRegistered(".osf"))      {        FileTypeRegInfo fileTypeRegInfo = new FileTypeRegInfo(".osf");        fileTypeRegInfo.Description = "OpenSelfFile文件";        fileTypeRegInfo.ExePath = Application.ExecutablePath;        fileTypeRegInfo.ExtendName = ".osf";        fileTypeRegInfo.IconPath = Application.ExecutablePath;         // 注册        FileTypeRegister fileTypeRegister = new FileTypeRegister();           FileTypeRegister.RegisterFileType(fileTypeRegInfo);    }  }

这样双击.osf的文件就可以打开对应的自定义应用程序了。

  相关解决方案