C#自定義文件圖標(關聯)

在我們自己編寫的應用中,經常會用自定義類型的文件的來保存與應用相關的數據,如何改變我們的自定義類型的文件的圖標?如何雙擊自定義類型文件的時候啓動相應的應用程序了?本文將告訴你如何通過程序來完成這些功能!
        比如.xcf文件就是XCodeFactory應用程序的項目文件。如果沒有向Windows註冊表註冊該文件類型,那麼.xcf文件的圖標將是windows的文件默認圖標,並且你雙擊一個a.xcf文件,也不會自動啓動XCodeFactory應用程序來加載a.xcf文件。如何使.xcf文件的圖標變成我自己喜愛的圖標、如何完成像點擊.doc文件就自動打開word程序的功能,下面將告訴你解決方案。

         我們可以通過手動修改註冊表來完成上述任務,更好的方式是,通過程序來實現。這樣,在安裝應用程序時,就可以自動的註冊自定義文件類型了。我通過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());
        }            
       關於自定義文件類型的註冊,本文實現的是最基本的功能,如果需要更多的高級功能,也可以類推實現之。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章