原文:http://blog.csdn.net/jiutao_tang/article/details/6563646

事实上有三种方式可以实现文件类型的注册,笔者在网上看到的都是手动实现的或程序实现的,其实也可以直接在工程属性里进行设置。

1. 工程属性定义

项目--->工程属性--->发布--->选项--->文件关联--->设置扩展名、说明、ProgID(自定义)、图标即可。

 

2. 手工实现文件类型关联

每一个文件类型的信息被保存在注册表中的 'HKEY_CLASSES_ROOT'下面。假设我们自定义的文件类型的后缀为.hyp,文件名为Test_File_Hype (中间不能有空格).

首先在HKEY_CLASSES_ROOT下创建 .hyp
HKEY_CLASSES_ROOT/.hyp

 

将[默认]键值改为"Test_File_Hype"。然后在HKEY_CLASSES_ROOT下添加主键 Test_File_Hype
HKEY_CLASSES_ROOT/Test_File_Hype

按照下面的路径添加新的主键 
HKEY_CLASSES_ROOT/Test_File_Hype/Shell
HKEY_CLASSES_ROOT/Test_File_Hype/Shell/Open
HKEY_CLASSES_ROOT/Test_File_Hype/Shell/Open/Command

将下面的字符作为Command的键值
     your application path.exe %1
     (例如 C:/WINDOWS/HYP/HYP.EXE %1)

或许你还想为自己的文件类型加上同自己的执行文件一样的图标,很简单,照下面的方法添加就行了。
HKEY_CLASSES_ROOT/Test_File_Hype/DefaultIcon
输入键值:
     your application path.EXE,0
后面的零表示文件的图标同程序的主图标一致,如果你的程序有很多图标,换一换数字就可改变文件显

参考:http://www.codesky.net/article/doc/200308/2003082482622669.htm

 

3.  C#编程实现自定义文件类型关联应用程序

在我们自己编写的应用中,经常会用自定义类型的文件的来保存与应用相关的数据,比如.xcf文件就是XCodeFactory应用程序的项目文件。如果没 有向Windows注册表注册该文件类型,那么.xcf文件的图标将是windows的文件默认图标,并且你双击一个a.xcf文件,也不会自动启动 XCodeFactory应用程序来加载a.xcf文件。如何使.xcf文件的图标变成我自己喜爱的图标、如何完成像点击.doc文件就自动打开word 程序的功能,下面将告诉你解决方案。 
    我们可以通过手动修改注册表来完成上述任务,更好的方式是,通过程序来实现。这样,在安装应用程序时,就可以自动的注册自定义文件类型 了。我通过FileTypeRegister静态类来完成这些功能。首先,将注册需要用到的信息封装成FileTypeRegInfo,定义如下:

 

[c-sharp] view plaincopy
 
  1. public class FileTypeRegInfo  
  2. {  
  3.     /// <summary>  
  4.     /// 目标类型文件的扩展名  
  5.     /// </summary>  
  6.     public string ExtendName;  //".xcf"  
  7.     /// <summary>  
  8.     /// 目标文件类型说明  
  9.     /// </summary>  
  10.     public string Description; //"XCodeFactory项目文件"  
  11.     /// <summary>  
  12.     /// 目标类型文件关联的图标  
  13.     /// </summary>  
  14.     public string IcoPath;  
  15.     /// <summary>  
  16.     /// 打开目标类型文件的应用程序  
  17.     /// </summary>  
  18.     public string ExePath;  
  19.     public FileTypeRegInfo()  
  20.     {  
  21.     }  
  22.     public FileTypeRegInfo(string extendName)  
  23.     {  
  24.         this.ExtendName = extendName;  
  25.     }  
  26. }  

 

 

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

 

 

[c-sharp] view plaincopy
 
  1. /// <summary>  
  2.     /// FileTypeRegister 用于注册自定义的文件类型。  
  3.     /// zhuweisky 2005.08.31  
  4.     /// </summary>  
  5.     public class FileTypeRegister  
  6.     {  
  7.         #region RegisterFileType  
  8.         /// <summary>  
  9.         /// RegisterFileType 使文件类型与对应的图标及应用程序关联起来。  
  10.         /// </summary>          
  11.         public static void RegisterFileType(FileTypeRegInfo regInfo)  
  12.         {  
  13.             if (FileTypeRegistered(regInfo.ExtendName))  
  14.             {  
  15.                 return;  
  16.             }  
  17.             string relationName = regInfo.ExtendName.Substring(1, regInfo.ExtendName.Length - 1).ToUpper() + "_FileType";  
  18.             RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName);  
  19.             fileTypeKey.SetValue("", relationName);  
  20.             fileTypeKey.Close();  
  21.             RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName);  
  22.             relationKey.SetValue("", regInfo.Description);  
  23.             RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon");  
  24.             iconKey.SetValue("", regInfo.IcoPath);  
  25.             RegistryKey shellKey = relationKey.CreateSubKey("Shell");  
  26.             RegistryKey openKey = shellKey.CreateSubKey("Open");  
  27.             RegistryKey commandKey = openKey.CreateSubKey("Command");  
  28.             commandKey.SetValue("", regInfo.ExePath + " %1");  
  29.             relationKey.Close();  
  30.         }  
  31.         /// <summary>  
  32.         /// GetFileTypeRegInfo 得到指定文件类型关联信息  
  33.         /// </summary>          
  34.         public static FileTypeRegInfo GetFileTypeRegInfo(string extendName)  
  35.         {  
  36.             if (!FileTypeRegistered(extendName))  
  37.             {  
  38.                 return null;  
  39.             }  
  40.             FileTypeRegInfo regInfo = new FileTypeRegInfo(extendName);  
  41.             string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType";  
  42.             RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName);  
  43.             regInfo.Description = relationKey.GetValue("").ToString();  
  44.             RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon");  
  45.             regInfo.IcoPath = iconKey.GetValue("").ToString();  
  46.             RegistryKey shellKey = relationKey.OpenSubKey("Shell");  
  47.             RegistryKey openKey = shellKey.OpenSubKey("Open");  
  48.             RegistryKey commandKey = openKey.OpenSubKey("Command");  
  49.             string temp = commandKey.GetValue("").ToString();  
  50.             regInfo.ExePath = temp.Substring(0, temp.Length - 3);  
  51.             return regInfo;  
  52.         }  
  53.         /// <summary>  
  54.         /// UpdateFileTypeRegInfo 更新指定文件类型关联信息  
  55.         /// </summary>      
  56.         public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo)  
  57.         {  
  58.             if (!FileTypeRegistered(regInfo.ExtendName))  
  59.             {  
  60.                 return false;  
  61.             }  
  62.   
  63.             string extendName = regInfo.ExtendName;  
  64.             string relationName = extendName.Substring(1, extendName.Length - 1).ToUpper() + "_FileType";  
  65.             RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName, true);  
  66.             relationKey.SetValue("", regInfo.Description);  
  67.             RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon", true);  
  68.             iconKey.SetValue("", regInfo.IcoPath);  
  69.             RegistryKey shellKey = relationKey.OpenSubKey("Shell");  
  70.             RegistryKey openKey = shellKey.OpenSubKey("Open");  
  71.             RegistryKey commandKey = openKey.OpenSubKey("Command", true);  
  72.             commandKey.SetValue("", regInfo.ExePath + " %1");  
  73.             relationKey.Close();  
  74.             return true;  
  75.         }  
  76.         /// <summary>  
  77.         /// FileTypeRegistered 指定文件类型是否已经注册  
  78.         /// </summary>          
  79.         public static bool FileTypeRegistered(string extendName)  
  80.         {  
  81.             RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName);  
  82.             if (softwareKey != null)  
  83.             {  
  84.                 return true;  
  85.             }  
  86.             return false;  
  87.         }  
  88.         #endregion  
  89.     }  

 

 

要注意的是commandKey.SetValue("" ,regInfo.ExePath + " %1") ;其中" %1"表示将被双击的文件的路径传给目标应用程序,这样在双击a.xcf文件时,XCodeFactory才知道要打开哪个文件,所以应用程序的Main方法要被改写为带有参数的形式,就像下面的样子:

 

[c-sharp] view plaincopy
 
  1. [STAThread]  
  2. static void Main(string[] args)  
  3. {  
  4.     if ((args != null) && (args.Length > 0))  
  5.     {  
  6.         string filePath = "";  
  7.         for (int i = 0; i < args.Length; i++)  
  8.         {  
  9.             filePath += " " + args[i];  
  10.         }  
  11.         MainForm.XcfFilePath = filePath.Trim();  
  12.     }  
  13.     Application.Run(new MainForm());  
  14. }  

 


关于自定义文件类型的注册,本文实现的是最基本的功能,如果需要更多的高级功能,也可以类推实现之。

注:

(1)应用程序的Main方法在Program.cs文件中,可以把原来无参的main方法注释掉;

(2)MainForm就是应用程序的启动窗体,可以改为自己的

(3)filePath.Trim() 就是获取的文件路径了

(4)using Microsoft.Win32;  //RegistryKey 位于 Microsoft.Win32 命名空间

(5)注册文件类型后,文件图标可能并不会立即生效(需注销登录或重启Explorer进程)。我们可以采取重启Explorer进程的方式使之立即生效,可参考示例程序中的代码。

 

我的一个实例:在Form_load事件中进行注册:

 

[c-sharp] view plaincopy
 
  1. private void Form1_Load(object sender, EventArgs e)  
  2.         {  
  3.             action = true;  
  4.             if (!FileTypeRegister.FileTypeRegistered(".hd")) //如果文件类型没有注册,则进行注册  
  5.             {  
  6.                 FileTypeRegInfo fileTypeRegInfo = new FileTypeRegInfo(".hd"); //文件类型信息  
  7.                 fileTypeRegInfo.Description = "巷道支护系统工程文件";  
  8.                 fileTypeRegInfo.ExePath = Application.ExecutablePath;  
  9.                 fileTypeRegInfo.ExtendName = ".hd";  
  10.                 fileTypeRegInfo.IcoPath = Application.ExecutablePath; //文件图标使用应用程序的  
  11.                 FileTypeRegister fileTypeRegister = new FileTypeRegister(); //注册  
  12.                 FileTypeRegister.RegisterFileType(fileTypeRegInfo);  
  13.                   
  14.                 Process[] process = Process.GetProcesses(); //重启Explorer进程,使更新立即生效  
  15.                 var p = (from proc in process  
  16.                          where proc.ProcessName.Equals("explorer")  
  17.                          select proc).FirstOrDefault();  
  18.                 p.Kill();  
  19.             }  
  20.         }  

 

 

参考:http://www.chenjiliang.com/Article/View.aspx?ArticleID=527&TypeID=79