C#使文件類型與程式關聯

来源:互联网 发布:帝国cms仿站 编辑:程序博客网 时间:2024/06/05 06:15

要注冊 自定義文件類型,包括文件關聯圖標,文件關聯應用程式等,這些操作都是通過修改注冊表來完成.

1.為便代碼清晰,首先將注冊需要的信息封裝到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;
        }
    }

2.編寫注冊文件類型的類.

   /// <summary>
    
/// FileTypeRegister 用于注册自定义的文件类型。
    
/// </summary>
    public class FileTypeRegister
    {
        
#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
    }

3.注意上段代碼中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());
        }   

4.通过文件的扩展名取得 與扩展名相对应的文件类型的图标

        [DllImport("Shell32.dll")]
        
static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, ref   SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
        
///   <summary>   
        
///   要用到的一个结构   
        
///   </summary>   
        struct SHFILEINFO
        {
            
public IntPtr hIcon;
            
public int iIcon;
            
public uint dwAttributes;
            
public char szDisplayName;
            
public char szTypeName;
        }

        
///   <summary>   
        
///   从文件扩展名得到小图标   
        
///   </summary>   
        
///   <param   name="FileName">文件名或文件扩展名</param>   
        
///   <returns>图标</returns>   
        static public Icon SetIcon(string FileName)
        {
            SHFILEINFO fi 
= new SHFILEINFO();
            Icon ic 
= null;
            
int iTotal = (int)SHGetFileInfo(FileName, 100ref fi, 0273);//从文件扩展名得到小图标   
            
//SHGFI_ICON+SHGFI_USEFILEATTRIBUTES+SmallIcon   
            
//273=十六进100   +10 +1   
            
//如果要大图标,改为272   
            if (iTotal > 0)
            {
                ic 
= Icon.FromHandle(fi.hIcon);
            }
            
return ic;
        }

5.總結

由於擔心破壞注冊表,所以以上代碼我並沒有做測試,但我仍然查看了一下我的注冊表中的內容,我用的是win2k,從注冊表看到,並沒有看到本文所寫的Shell,Open,Command等鍵,具體是什麼結構我也沒看出來,估計文件關聯與操作系統的不同也有關系,這篇文章暫且寄存在此,等以後需要時再做詳細研究.

參考文章: http://www.gissky.com/Develop/ShowArticle.Asp?Sid=21&ID=1523 及http://topic.csdn.net/t/20060921/22/5039676.html

原创粉丝点击