Click here to Skip to main content
15,880,469 members
Articles / Desktop Programming / MFC
Article

Associate File Extension with Shell OPEN command and Application

Rate me:
Please Sign up or sign in to vote.
4.76/5 (17 votes)
22 Jun 2000 254.9K   5K   54   36
Registry entries and MFC class that associates a file extension with a program.
  • Download source files - 3 Kb
  • Download demo project - 17 Kb
  • Introduction

    This source contains a class CGCFileTypeAccess that can associate a file extension with a program.

    The association allows a new instance of the program to be activated when the icon is clicked upon in the Windows Explorer window. The full file path to the file will be sent to the program on the command line.

    The Open command also appears on the shell context menu for the file type. Selection of the open command from the context menu sends the file on the command line to a new instance of the program.

    An example of associating a file extension using the class would be done as follows, and can be found in the InitInstance of the demo program:

    CGCFileTypeAccess TheFTA;
    
    // get full file path to program executable file
    TCHAR	szProgPath[MAX_PATH * 2];
    ::GetModuleFileName(NULL, szProgPath, sizeof(szProgPath)/sizeof(TCHAR));
    
    CString csTempText;
    
    TheFTA.SetExtension("CGFILETYPETEST");
    
    // just pass file path in quotes on command line
    csTempText  = szProgPath;
    csTempText += " \"%1\"";
    TheFTA.SetShellOpenCommand(csTempText);
    TheFTA.SetDocumentShellOpenCommand(csTempText);
    
    TheFTA.SetDocumentClassName("CGFileType.Document");
    
    // use first icon in program
    csTempText  = szProgPath;
    csTempText += ",0";
    TheFTA.SetDocumentDefaultIcon(csTempText);
    
    // set the necessary registry entries	
    TheFTA.RegSetAllInfo();

    The relevant registry entries are all under HKEY_CLASSES_ROOT. The two primary root keys are .'FileExtension' and 'Document Class Name'. Keys and values underneath these two primary keys control the file association and the shell open command.

    The demo program will execute the file association code listed above. After it has been run at least once, each time you 'open' one of the associated file types, the program will display the file path from the command line in a message box.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    Web Developer
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralRe: Multiple Files TOgether ??? Pin
    tot2ivn24-Feb-05 17:47
    tot2ivn24-Feb-05 17:47 
    QuestionAutomatically open document extension? Pin
    AAntix10-Jan-05 23:00
    AAntix10-Jan-05 23:00 
    AnswerRe: Automatically open document extension? Pin
    Blake V. Miller18-Jan-05 17:20
    Blake V. Miller18-Jan-05 17:20 
    Generalfile names Pin
    brian scott14-Apr-04 6:23
    brian scott14-Apr-04 6:23 
    GeneralRe: file names Pin
    Blake V. Miller18-Jan-05 17:11
    Blake V. Miller18-Jan-05 17:11 
    GeneralRe: file names Pin
    cabaa992-Aug-05 9:50
    cabaa992-Aug-05 9:50 
    GeneralThe Next Steps Pin
    John Ulvr10-Apr-03 2:09
    John Ulvr10-Apr-03 2:09 
    GeneralRe: The Next Steps Pin
    Blake V. Miller18-Jan-05 17:16
    Blake V. Miller18-Jan-05 17:16 
    Generallong file name problem Pin
    11-Mar-02 23:51
    suss11-Mar-02 23:51 
    GeneralRe: long file name problem Pin
    Blake V. Miller18-Jan-05 17:18
    Blake V. Miller18-Jan-05 17:18 
    GeneralRe: long file name problem Pin
    Sreekanth Muralidharan28-Nov-05 17:33
    Sreekanth Muralidharan28-Nov-05 17:33 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.