Click here to Skip to main content
15,175,307 members
Articles / Desktop Programming / MFC
Posted 22 Jun 2000


54 bookmarked

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
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;
    // just pass file path in quotes on command line
    csTempText  = szProgPath;
    csTempText += " \"%1\"";
    // use first icon in program
    csTempText  = szProgPath;
    csTempText += ",0";
    // set the necessary registry entries	

    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.


    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
    No Biography provided

    Comments and Discussions

    QuestionError Access Denied? Pin
    bosfan10-Jul-13 1:27
    Memberbosfan10-Jul-13 1:27 
    QuestionGet path of a file clicked upon in windows explorer. Pin
    Mahdi Nejadsahebi14-Nov-12 22:22
    MemberMahdi Nejadsahebi14-Nov-12 22:22 
    GeneralMy vote of 5 Pin
    Mahdi Nejadsahebi14-Nov-12 22:09
    MemberMahdi Nejadsahebi14-Nov-12 22:09 
    Questionwin7 Pin
    lukegorman8-Dec-11 0:17
    Memberlukegorman8-Dec-11 0:17 
    QuestionHow to change the default icon index Pin
    krishnakumartm27-Jun-11 1:22
    Memberkrishnakumartm27-Jun-11 1:22 
    GeneralDoesn't work Pin
    dpreznik12-May-11 5:01
    Memberdpreznik12-May-11 5:01 
    GeneralDoes not work if the user has specified another default application Pin
    dragomir5-Mar-09 7:20
    Memberdragomir5-Mar-09 7:20 
    QuestionOpen file error Pin
    Rui Frazao26-Dec-06 7:51
    MemberRui Frazao26-Dec-06 7:51 
    GeneralDelphi applications Pin
    Syhon23-Nov-06 22:12
    MemberSyhon23-Nov-06 22:12 
    GeneralCool... now how do you receive the open command... Pin
    M i s t e r L i s t e r16-Oct-06 13:53
    MemberM i s t e r L i s t e r16-Oct-06 13:53 
    GeneralRe: Cool... now how do you receive the open command... Pin
    BadJerry2-Sep-09 1:04
    MemberBadJerry2-Sep-09 1:04 
    GeneralSimple C# version Pin
    Cristian Scutaru21-Jul-06 10:14
    MemberCristian Scutaru21-Jul-06 10:14 
    Usefull stuff, Blake! Here is a simple C# version, to be adapted to your needs (check extension pre-conditions etc):

    <br />
    using Microsoft.Win32;<br />
    <br />
    // Associate file extension with progID, description, icon and application<br />
    public static void Associate(string extension, string progID, string description, string icon, string application)<br />
    {<br />
       Registry.ClassesRoot.CreateSubKey(extension).SetValue("", progID);<br />
       if (progID!=null && progID.Length>0)<br />
          using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID))<br />
          {<br />
             if (description!=null)<br />
                key.SetValue("", description);<br />
             if (icon!=null)<br />
                key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(icon));<br />
             if (application!=null)<br />
                key.CreateSubKey(@"Shell\Open\Command").SetValue("", ToShortPathName(application) + " \"%1\"");<br />
          }<br />
    }<br />
    <br />
    // Return true if extension already associated in registry<br />
    public static bool IsAssociated(string extension)<br />
    {<br />
       return (Registry.ClassesRoot.OpenSubKey(extension, false)!=null);<br />
    }<br />
    <br />
    [DllImport("Kernel32.dll")]<br />
    private static extern uint GetShortPathName(string lpszLongPath, [Out] StringBuilder lpszShortPath, uint cchBuffer);<br />
    <br />
    // Return short path format of a file name<br />
    private static string ToShortPathName(string longName)<br />
    {<br />
       StringBuilder s = new StringBuilder(1000);<br />
       uint iSize = (uint) s.Capacity;<br />
       uint iRet = GetShortPathName(longName, s, iSize);<br />
       return s.ToString();<br />
    }<br />

    Call pattern:

    <br />
    if (!IsAssociated(".ext"))<br />
       Associate(".ext", "ClassID.ProgID", "ext File", "YourIcon.ico", "YourApplication.exe");<br />

    QuestionRe: Simple C# version Pin
    FrEaK_CH25-Sep-06 8:35
    MemberFrEaK_CH25-Sep-06 8:35 
    AnswerRe: Simple C# version Pin
    Cristian Scutaru26-Sep-06 16:27
    MemberCristian Scutaru26-Sep-06 16:27 
    GeneralLong files names Pin
    Ketinla26-Jan-06 1:21
    MemberKetinla26-Jan-06 1:21 
    Questionhow to check... Pin
    creative0027-Nov-05 21:20
    Membercreative0027-Nov-05 21:20 
    GeneralSetDocumentDefaultIcon Pin
    ehh15-Nov-05 8:49
    Memberehh15-Nov-05 8:49 
    GeneralRe: SetDocumentDefaultIcon Pin
    Blake V. Miller24-Nov-05 10:06
    MemberBlake V. Miller24-Nov-05 10:06 
    GeneralThanks a lot... Pin
    Sreekanth Muralidharan13-Oct-05 20:07
    MemberSreekanth Muralidharan13-Oct-05 20:07 
    QuestionHow to tell the explorer refresh the icon? Pin
    Michael Leung24-Jul-05 2:36
    MemberMichael Leung24-Jul-05 2:36 
    AnswerRe: How to tell the explorer refresh the icon? Pin
    po.coulet14-Sep-05 3:51
    Memberpo.coulet14-Sep-05 3:51 
    GeneralRe: How to tell the explorer refresh the icon? Pin
    Mandrago10-Jul-07 3:44
    MemberMandrago10-Jul-07 3:44 
    GeneralRe: How to tell the explorer refresh the icon? Pin
    vigylant18-Jan-09 8:46
    Membervigylant18-Jan-09 8:46 
    QuestionMultiple Files TOgether ??? Pin
    tot2ivn18-Feb-05 1:16
    Membertot2ivn18-Feb-05 1:16 
    AnswerRe: Multiple Files TOgether ??? Pin
    Blake V. Miller24-Feb-05 17:12
    MemberBlake V. Miller24-Feb-05 17:12 

    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.