C # Registering custom file types Implementing a custom file type affiliate application

Source: Internet
Author: User

In our own applications, custom types of files are often used to store data related to the application, such as the. osf file, which is the application's project file. If the file type is not registered with the Windows registry, then. The icon for the OSF file will be the default icon for the Windows file, and you double-click a. The OSF file does not automatically start the application to load a. OSF file. How to make. The icon of the OSF file becomes the icon of my own favorite, how to complete the function like click. doc file to automatically open the Word program, the following will tell you the solution.


We can do this by manually modifying the registry, and a better way to do this is through a program. This allows you to automatically register the custom file type when you install the application. I do these functions by Filetyperegister static classes. First, the information required for registration is encapsulated into Filetypereginfo, defined as follows:

<summary>///File type registration information//</summary>public class filetypereginfo{//<summary>///      extension ///      </summary> public      string extendname;  ". OSF"      ///<summary>//Description///</summary> public string Description;//" Openselffile project File "///<summary>//////      </summary> public      string IconPath;    <summary>///Applications///      </summary> public      string ExePath;    Public Filetypereginfo ()    {    } public    Filetypereginfo (string extendname)    {this        . Extendname = Extendname;    }}


The Filetyperegister class is primarily about manipulating the contents of the registry, as follows:

<summary>///Register a custom file type. </summary> public class filetyperegister{//<summary>//To associate file types with corresponding icons and applications///</summ ary> public static void Registerfiletype (Filetypereginfo reginfo) {if (filetyperegistered (Regin Fo.        Extendname)) {return; }//HKEY_CLASSES_ROOT/.OSF RegistryKey Filetypekey = Registry.ClassesRoot.CreateSubKey (reginfo.exten        DNAME); String relationname = regInfo.ExtendName.Substring (1, REGINFO.E XTENDNAME.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" means that the path of the file being double-clicked is passed to the target application relationkey.close (); }///<summary>//Update specify file type association information///</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>///Get the specified file type association information///</summary> public static Filetypereginfo Getfil Etypereginfo (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>//Specifies whether the file type is already registered///</summary> public static bool Filetyperegist        Ered (String extendname) {RegistryKey Softwarekey = Registry.ClassesRoot.OpenSubKey (extendname);        if (Softwarekey! = null) {return true;    } return false;   }}

The main method of the application is to be rewritten as a parameter, as in the following manner:
Using system;using system.collections.generic;using system.linq;using system.threading.tasks;using System.windows.forms;namespace openselffile{    Static class program    {//        <summary>        /// The main entry point for the application. --plus string[] args parameter//        </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 Parameters                }                Mainwnd._osffilepath = Filepath.trim ();            }              Application.enablevisualstyles ();            Application.setcompatibletextrenderingdefault (false);            Application.Run (New Mainwnd ());}}}    

to register in the Form_Load event:

private void Form1_Load (object sender, EventArgs e) {    if (! Filetyperegister.filetyperegistered (". OSF"))      {        Filetypereginfo filetypereginfo = new Filetypereginfo (". OSF ");        filetypereginfo.description = "Openselffile file";        Filetypereginfo.exepath = Application.executablepath;        Filetypereginfo.extendname = ". OSF";        Filetypereginfo.iconpath = Application.executablepath;         Register        Filetyperegister filetyperegister = new Filetyperegister ();           Filetyperegister.registerfiletype (Filetypereginfo);    }  }

This allows you to open the corresponding custom application by double-clicking the. osf file.

C # Registering custom file types Implementing a custom file type affiliate application

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.