Click here to Skip to main content
Click here to Skip to main content

System File Association

By , 16 Mar 2007
 

Introduction

Ever wanted to programmatically associate a file type on the system with your application, but didn't like the idea of digging through the registry yourself? If so, then this article and code are right for you.

Background

File associations in Windows have two parts, the extension itself and the ProgID (programmatic identifier). While an extension does not have to be associated with any ProgID, if it is, it can only be associated with a single one. On the other hand, a ProgID can have multiple extensions associated with it.

The attached code includes classes such as:

  • FileAssociationInfo: provides properties to determine (or set) what ProgID the extension is associated with (ProgID), what sort of file the system considers it to be (PerceivedType), the MIME type of the file (ContentType), and what programs will appear in the extensions (OpenWithList).
  • ProgramAssociationInfo: functions similarly to FileAssociationInfo and provides properties to set how the shell should handle the file type (EditFlags), the command verbs and programs the ProgID supports (Verbs), and the file types icon (DefaultIcon).
  • AssociationManager: provides a simplistic method to determine if certain extensions are associated with a given ProgID. It also provides the ability to associate those types or to create a brand new association between already specified extensions and a ProgID.

Examples

Our first step is to create an instance of the FileAssociationInfo class and specify the extension we wish to deal with into the constructor. Next we see if the extension already exists and if it doesn't, we create it with the specified ProgID (MyProgramName), and then set up the optional ContentType and OpenWithList properties.

FileAssociationInfo fai = new FileAssociationInfo(".bob");
    if (!fai.Exists)
      {
         fai.Create("MyProgramName");

         //Specify MIME type (optional)
         fai.ContentType = "application/myfile";

         //Programs automatically displayed in open with list
         fai.OpenWithList = new string[]
        { "notepad.exe", "wordpad.exe", "someotherapp.exe" };
       }

Finally, we create an instance of the ProgramAssociationInfo class and specify the ProgID we wish to deal with in its constructor. Should this ProgID not exist, we create it and specify both a description for the program type (shared between all files using this ProgID) and the command verb that is used in selecting different ways to load the file.

ProgramAssociationInfo pai = new ProgramAssociationInfo(fai.ProgID);
    if (!pai.Exists)
      {
         pai.Create
         (
         //Description of program/file type
         "My Program's File Type",

         new ProgramVerb
              (
              //Verb name
              "Open",
              //Path and arguments to use
              @"C:\SomePath\MyApp.exe %1"
              )
            );

         //optional
         pai.DefaultIcon = new ProgramIcon(@"C:\SomePath\SomeIcon.ico");
       }

Full sample

The link at the top of this article includes a simplistic GUI that demonstrates all the capabilities of the FileAssociationInfo and ProgramAssociationInfo classes.

Word of warning

This code requires administrative access (especially under Vista) when used to create or modify extensions, and on some systems, it also requires elevated permission to read.

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

Brendan Grant
Web Developer
United States United States
Member
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionpdf, txtmemberPhilip Stuyck31 Jan '13 - 8:45 
QuestionHow to replace an existing extension?memberalexander200531 Dec '12 - 6:38 
QuestionCan't open multiple filesmemberFabrizio Stellato2 Dec '12 - 23:39 
NewsNuGet PackagememberRob Vesse2 Nov '12 - 9:29 
QuestionDoesnt work with Win7memberHrizip5 Dec '11 - 2:50 
Question%1 argumentmemberBence Sebők7 Jun '11 - 8:43 
AnswerRe: %1 argumentmemberDimitris Vassiliades7 Jun '11 - 10:52 
GeneralMy vote of 5memberMario Majcica19 May '11 - 11:26 
GeneralGreat articlememberjatinsehgal8618 May '11 - 2:51 
AnswerProgramIcon bug fix!memberWaldo Bronchart1 Mar '11 - 2:27 
GeneralRe: ProgramIcon bug fix!memberikkentim31 Jan '12 - 10:35 
GeneralSome notes about file extensions on windowsmemberTheToid12 May '10 - 13:47 
GeneralGreat post, one commentmemberLimitedAtonement3 Feb '10 - 10:02 
Generalthanks for the codememberwas830917 Sep '09 - 18:02 
Smile | :)
GeneralAssociations not Working for currently associated filesmemberPipSharp18 Aug '09 - 0:36 
GeneralRe: Associations not Working for currently associated filesmemberLimitedAtonement3 Feb '10 - 10:03 
GeneralRe: Associations not Working for currently associated filesmemberTheToid12 May '10 - 13:51 
QuestionI want to use your code in my app. Can I ?memberIQ719 Jun '09 - 3:27 
QuestionHow to run this applicationmemberKapilDesai17 May '09 - 22:42 
QuestionHow to use it for an applicationmemberMichael Egan9 Apr '09 - 22:46 
Generaldoes not work under VISTAmemberviewon013 Apr '08 - 10:59 
GeneralFilenames with spacesmemberpucis8324 Feb '08 - 13:15 
QuestionHow to get icon from ProgramAssociationInfo.DefaultIconmemberhoanhtuan8 Jan '08 - 21:34 
AnswerRe: How to get icon from ProgramAssociationInfo.DefaultIconmemberAschratt31 Jan '08 - 7:51 
GeneralDefaultIcon Bug [modified]memberAschratt1 Dec '07 - 23:16 
GeneralRe: DefaultIcon Bugmemberzackflame15 Jun '10 - 4:48 
GeneralAccess to registry key deniedmemberkamal2003us1 Jul '07 - 23:49 
GeneralRe: Access to registry key deniedmemberBrendan Grant2 Jul '07 - 5:24 
GeneralRe: Access to registry key deniedmemberyf0129 Dec '07 - 20:39 
GeneralMay not need admin access under VistamemberThe_Mega_ZZTer16 Mar '07 - 14:42 
QuestionGreat stuff! Can I use it?memberRichard Brightwell8 Mar '07 - 8:27 
AnswerRe: Great stuff! Can I use it?memberBrendan Grant12 Mar '07 - 6:18 
GeneralBrief version [modified]memberMatthiasBiel21 Feb '07 - 20:41 
GeneralRe: Brief versionmemberTesto28ß519 Jun '08 - 10:45 
GeneralRe: Brief versionmemberkasparovthe218 Feb '09 - 6:33 
GeneralRe: Brief versionmemberspamblot13 Mar '09 - 4:51 
GeneralRe: Brief versionmemberWayne Walter6 May '10 - 8:26 
GeneralHey BrendanmemberTall Dude9 Jan '07 - 20:20 
GeneralRe: Hey BrendanmemberBrendan Grant10 Jan '07 - 4:59 
GeneralRe: Hey BrendanmemberTall Dude10 Jan '07 - 8:51 
GeneralRe: Hey BrendanmemberBrendan Grant21 Mar '07 - 17:45 
GeneralRe: Hey BrendanmemberTall Dude22 Mar '07 - 13:04 
GeneralNice code!memberRynus9 Jan '07 - 3:41 
GeneralRe: Nice code!memberBrendan Grant10 Jan '07 - 4:57 
GeneralGreat [modified]memberDrJaymz5 Jan '07 - 6:38 
GeneralRe: GreatmemberBrendan Grant10 Jan '07 - 4:55 
GeneralRe: GreatmemberDrJaymz10 Jan '07 - 5:59 
GeneralRe: GreatmemberBrendan Grant10 Jan '07 - 10:19 
GeneralRe: GreatmemberDiamonddrake29 May '09 - 9:46 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 16 Mar 2007
Article Copyright 2007 by Brendan Grant
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid