C # automatically register custom file types

Source: Internet
Author: User

In fact, there are three ways to register file types. what I see on the Internet is manually implemented orProgramIn fact, you can also directly set it in the project properties.

1. Project property Definition

Project ---> project properties ---> release ---> options ---> File Association ---> set the extension, description, progid (custom), and icon.

 

2. Manually associate file types

Information about each file type is stored under 'HKEY _ classes_root 'in the registry. Assume that the custom file type is suffixed with. Hyp and the file name is test_file_hype (spaces are not allowed in the middle ).

First, create. Hyp under hkey_classes_root.
Hkey_classes_root/. Hyp

 

Change the [Default] key value to "test_file_hype ". Then add the primary key test_file_hype under hkey_classes_root.
Hkey_classes_root/test_file_hype

Add a new primary key according to the following path
Hkey_classes_root/test_file_hype/Shell
Hkey_classes_root/test_file_hype/Shell/Open
Hkey_classes_root/test_file_hype/Shell/Open/command

Use the following characters as the key value of command.
Your application path.exe % 1
(For example, C:/Windows/Hyp. EXE % 1)

Maybe you want to add the same icon for your file type as your execution file. It's easy to add it as follows.
Hkey_classes_root/test_file_hype/defaulticon
Input key value:
Your application path. EXE, 0
The zero icon next to the file is the same as the main icon of the program. If your program has many icons, you can change the number to change the file display.

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

 

3. C # program and implement Application Association of custom file types

In our own applications, we often use custom files to store application-related data. For example, the. xcf file is the project file of the xcodefactory application. If no
If you have registered the file type with the Windows registry, the. xcf file icon will be the default icon for Windows files, and you double-click a. xcf file and it will not start automatically
Xcodefactory application to load the. xcf file. For example, how can I convert the graph of the. xcf file into a cute graph? For example, click the. DOC file to open the word automatically
The function of the program. The following describes the solution.

You can manually modify the Registry to complete the preceding tasks. A better way is to use a program. In this way, you can automatically register the custom file type when installing the application.
. I use the filetyperegister static class to complete these functions. First, encapsulate the information needed for registration into filetypereginfo, which is defined as follows:

Public class filetypereginfo <br/>{< br/> /// <summary> <br/> // extension of the target type file <br/> /// </Summary> <br/> Public String extendname; //". xcf "<br/> /// <summary> <br/> // description of the target file type <br/> /// </Summary> <br/> Public String description; // "xcodefactory project file" <br/> /// <summary> <br/> // icon associated with the target type file <br/> /// </Summary> <br/> Public String icopath; <br/> /// <summary> <br/> // open the application of the target type file <br/> /// </Summary> <br/> Public string exepath; <br/> Public filetypereginfo () <br/>{< br/>}< br/> Public filetypereginfo (string extendname) <br/>{< br/> This. extendname = extendname; <br/>}< br/>}

 

The filetyperegister class is mainly used to operate the content in the registry. The implementation is as follows:

 

/// <Summary> <br/> // filetyperegister is used to register a custom file type. <Br/> // zhuweisky 2005.08.31 <br/> // </Summary> <br/> public class filetyperegister <br/> {<br/> # region registerfiletype <br/> /// <summary> <br/> // registerfiletype associates the file type with the corresponding icon and application. <Br/> // </Summary> <br/> Public static void registerfiletype (filetypereginfo reginfo) <br/>{< br/> If (filetyperegistered (reginfo. extendname) <br/>{< br/> return; <br/>}< br/> string relationname = reginfo. extendname. substring (1, reginfo. extendname. length-1 ). toupper () + "_ filetype"; <br/> registrykey filetypekey = registry. classesroot. createsubkey (reginfo. extendname); <br/> filetypekey. set Value ("", relationname); <br/> filetypekey. close (); <br/> registrykey relationkey = registry. classesroot. createsubkey (relationname); <br/> relationkey. setvalue ("", reginfo. description); <br/> registrykey iconkey = relationkey. createsubkey ("defaulticon"); <br/> iconkey. setvalue ("", reginfo. icopath); <br/> registrykey shellkey = relationkey. createsubkey ("shell"); <br/> registrykey openkey = shellkey. Createsubkey ("open"); <br/> registrykey commandkey = openkey. createsubkey ("command"); <br/> commandkey. setvalue ("", reginfo. exepath + "% 1"); <br/> relationkey. close (); <br/>}< br/> /// <summary> <br/> // getfiletypereginfo to obtain the association information of the specified file type. <br/> /// </Summary> <br/> Public static filetypereginfo getfiletypereginfo (string extendname) <br/>{< br/> If (! Filetyperegistered (extendname) <br/>{< br/> return NULL; <br/>}< br/> filetypereginfo reginfo = new filetypereginfo (extendname ); <br/> string relationname = extendname. substring (1, extendname. length-1 ). toupper () + "_ filetype"; <br/> registrykey relationkey = registry. classesroot. opensubkey (relationname); <br/> reginfo. description = relationkey. getvalue (""). tostring (); <br/> registrykey iconkey = Relationkey. opensubkey ("defaulticon"); <br/> reginfo. icopath = iconkey. getvalue (""). tostring (); <br/> registrykey shellkey = relationkey. opensubkey ("shell"); <br/> registrykey openkey = shellkey. opensubkey ("open"); <br/> registrykey commandkey = openkey. opensubkey ("command"); <br/> string temp = commandkey. getvalue (""). tostring (); <br/> reginfo. exepath = temp. substring (0, temp. length-3); <br/> Return reginfo; <br/>}< br/> /// <summary> <br/> // updatefiletypereginfo updates the association information of the specified file type. <br/> /// </Summary> <br/> Public static bool updatefiletypereginfo (filetypereginfo reginfo) <br/>{< br/> If (! Filetyperegistered (reginfo. extendname) <br/>{< br/> return false; <br/>}</P> <p> string extendname = reginfo. extendname; <br/> string relationname = extendname. substring (1, extendname. length-1 ). toupper () + "_ filetype"; <br/> registrykey relationkey = registry. classesroot. opensubkey (relationname, true); <br/> relationkey. setvalue ("", reginfo. description); <br/> registrykey iconkey = relationkey. Opensubkey ("defaulticon", true); <br/> iconkey. setvalue ("", reginfo. icopath); <br/> registrykey shellkey = relationkey. opensubkey ("shell"); <br/> registrykey openkey = shellkey. opensubkey ("open"); <br/> registrykey commandkey = openkey. opensubkey ("command", true); <br/> commandkey. setvalue ("", reginfo. exepath + "% 1"); <br/> relationkey. close (); <br/> return true; <br/>}< br/> // <summary> <br/> /// Filetyperegistered specifies whether the file type has been registered <br/> /// </Summary> <br/> Public static bool filetyperegistered (string extendname) <br/>{< br/> registrykey softwarekey = registry. classesroot. opensubkey (extendname); <br/> If (softwarekey! = NULL) <br/>{< br/> return true; <br/>}< br/> return false; <br/>}< br/> # endregion <br/>}

 

Note the commandkey. setvalue ("", reginfo. exepath + "% 1"); "% 1" indicates that the path of the file to be double-clicked is passed to the target application. xcodefactory knows which file to open when the xcf file is used. Therefore, the main method of the application must be rewritten as a parameter, as shown below:

[Stathread] <br/> static void main (string [] ARGs) <br/>{< br/> If (ARGs! = NULL) & (ARGs. length> 0) <br/>{< br/> string filepath = ""; <br/> for (INT I = 0; I <args. length; I ++) <br/>{< br/> filepath + = "" + ARGs [I]; <br/>}< br/> mainform. xcffilepath = filepath. trim (); <br/>}< br/> application. run (New mainform (); <br/>}

For registration of custom file types, this article implements the most basic functions. If you need more advanced functions, you can also implement them by analogy.

Note:

(1) The main method of the application is in the program. CS file. You can comment out the main method without parameters;

(2) mainform is the startup form of the application. You can change it to your own

(3) filepath. Trim () is the obtained file path.

(4) using Microsoft. Win32; // registrykey is located in the Microsoft. Win32 namespace

(5) After the file type is registered, the file icon may not take effect immediately (You need to log out or restart the explorer process ). You can restart the explorer process to make it take effect immediately. For more information, seeCode.

 

One of my instances: register in the form_load event:

Private void form1_load (Object sender, eventargs e) <br/>{< br/> action = true; <br/> If (! Filetyperegister. filetyperegistered (". HD ") // if the file type is not registered, register it <br/>{< br/> filetypereginfo = new filetypereginfo (". HD "); // file type information <br/> filetypereginfo. description = "Roadway Support System Engineering file"; <br/> filetypereginfo. exepath = application. executablepath; <br/> filetypereginfo. extendname = ". HD "; <br/> filetypereginfo. icopath = application. executablepath; // file icon used by the application <br/> filetyperegister = new filetyperegister (); // register <br/> filetyperegister. registerfiletype (filetypereginfo); </P> <p> process [] process = process. getprocesses (); // restart the explorer process to make the update take effect immediately <br/> var P = (from proc in Process <br/> where Proc. processname. equals ("Explorer") <br/> select proc ). firstordefault (); <br/> P. kill (); <br/>}< br/>}

 

Reference: http://www.chenjiliang.com/Article/View.aspx? ArticleID = 527 & typeid = 79

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.