C# 註冊自訂檔案類型 實現自訂檔案類型關聯應用程式

來源:互聯網
上載者:User

標籤:註冊表

    在我們自己編寫的應用中,經常會用自訂類型的檔案來儲存與應用相關的資料,比如.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的檔案就可以開啟對應的自訂應用程式了。

C# 註冊自訂檔案類型 實現自訂檔案類型關聯應用程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.