Click here to Skip to main content
Licence 
First Posted 22 Jun 2000
Views 162,612
Bookmarked 48 times

Associate File Extension with Shell OPEN command and Application

By | 22 Jun 2000 | Article
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

    About the Author

    Blake V. Miller

    Web Developer

    United States United States

    Member



    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board. (secure sign-in)
     
    Search this forum  
     FAQ
        Noise  Layout  Per page   
      Refresh
    Questionwin7 PinmemberMember 806538823:17 7 Dec '11  
    QuestionHow to change the default icon index Pinmemberkrishnakumartm0:22 27 Jun '11  
    GeneralDoesn't work Pinmemberdpreznik4:01 12 May '11  
    GeneralDoes not work if the user has specified another default application Pinmemberdragomir6:20 5 Mar '09  
    QuestionOpen file error PinmemberRui Frazão6:51 26 Dec '06  
    GeneralDelphi applications PinmemberSyhon21:12 23 Nov '06  
    GeneralCool... now how do you receive the open command... PinmemberM i s t e r L i s t e r12:53 16 Oct '06  
    GeneralRe: Cool... now how do you receive the open command... PinmemberBadJerry0:04 2 Sep '09  
    GeneralSimple C# version Pinmembercristiscu9:14 21 Jul '06  
    QuestionRe: Simple C# version PinmemberFrEaK_CH7:35 25 Sep '06  
    AnswerRe: Simple C# version Pinmembercristiscu15:27 26 Sep '06  
    ClassID.ProgID is the identifier of an ActiveX/COM component (see spec).
     
    I'm not sure, but it seems that an application must be registered as COM to make this kind of file association possible Confused | :confused: (?)
    GeneralLong files names PinmemberKetinla0:21 26 Jan '06  
    Questionhow to check... Pinmembercreative0020:20 27 Nov '05  
    GeneralSetDocumentDefaultIcon Pinmemberehh7:49 15 Nov '05  
    GeneralRe: SetDocumentDefaultIcon PinmemberBlake V. Miller9:06 24 Nov '05  
    GeneralThanks a lot... PinmemberSreekanth Muralidharan19:07 13 Oct '05  
    QuestionHow to tell the explorer refresh the icon? PinmemberMichael Leung1:36 24 Jul '05  
    AnswerRe: How to tell the explorer refresh the icon? Pinmemberpo.coulet2:51 14 Sep '05  
    GeneralRe: How to tell the explorer refresh the icon? PinmemberMandrago2:44 10 Jul '07  
    GeneralRe: How to tell the explorer refresh the icon? Pinmembervigylant7:46 18 Jan '09  
    QuestionMultiple Files TOgether ??? Pinmembertot2ivn0:16 18 Feb '05  
    AnswerRe: Multiple Files TOgether ??? PinmemberBlake V. Miller16:12 24 Feb '05  
    GeneralRe: Multiple Files TOgether ??? Pinmembertot2ivn17:47 24 Feb '05  
    QuestionAutomatically open document extension? PinmemberAAntix23:00 10 Jan '05  
    AnswerRe: Automatically open document extension? PinmemberBlake V. Miller17:20 18 Jan '05  

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

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

    Permalink | Advertise | Privacy | Mobile
    Web01 | 2.5.120529.1 | Last Updated 23 Jun 2000
    Article Copyright 2000 by Blake V. Miller
    Everything else Copyright © CodeProject, 1999-2012
    Terms of Use
    Layout: fixed | fluid