Click here to Skip to main content
11,430,039 members (72,589 online)
Click here to Skip to main content

System File Association

, 16 Mar 2007
Rate this:
Please Sign up or sign in to vote.
This article describes the use of several custom C# classes that can be used to create, view, edit and delete Windows file associations.

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

Share

About the Author

Brendan Grant
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
Questionpdf, txt Pin
Philip Stuyck31-Jan-13 9:45
memberPhilip Stuyck31-Jan-13 9:45 
QuestionHow to replace an existing extension? Pin
alexander200531-Dec-12 7:38
memberalexander200531-Dec-12 7:38 
QuestionCan't open multiple files Pin
Fabrizio Stellato3-Dec-12 0:39
memberFabrizio Stellato3-Dec-12 0:39 
NewsNuGet Package Pin
Rob Vesse2-Nov-12 10:29
memberRob Vesse2-Nov-12 10:29 
For those who are interested I created a NuGet package for this library to make it easier to pull into newer projects

The code for creating the NuGet package is on BitBucket and you can find the package on NuGet
QuestionDoesnt work with Win7 Pin
Hrizip5-Dec-11 3:50
memberHrizip5-Dec-11 3:50 
Question%1 argument Pin
Bence Sebők7-Jun-11 9:43
memberBence Sebők7-Jun-11 9:43 
AnswerRe: %1 argument Pin
Dimitris Vassiliades7-Jun-11 11:52
memberDimitris Vassiliades7-Jun-11 11:52 
AnswerRe: %1 argument Pin
Afzaal Ahmad Zeeshan4-Oct-14 7:52
professionalAfzaal Ahmad Zeeshan4-Oct-14 7:52 
GeneralMy vote of 5 Pin
Mario Majcica19-May-11 12:26
memberMario Majcica19-May-11 12:26 
GeneralGreat article Pin
jatinsehgal8618-May-11 3:51
memberjatinsehgal8618-May-11 3:51 
AnswerProgramIcon bug fix! Pin
Waldo Bronchart1-Mar-11 3:27
memberWaldo Bronchart1-Mar-11 3:27 
GeneralRe: ProgramIcon bug fix! Pin
ikkentim31-Jan-12 11:35
memberikkentim31-Jan-12 11:35 
GeneralSome notes about file extensions on windows Pin
TheToid12-May-10 14:47
memberTheToid12-May-10 14:47 
GeneralGreat post, one comment Pin
LimitedAtonement3-Feb-10 11:02
memberLimitedAtonement3-Feb-10 11:02 
Generalthanks for the code Pin
was830917-Sep-09 19:02
memberwas830917-Sep-09 19:02 
GeneralAssociations not Working for currently associated files Pin
PipSharp18-Aug-09 1:36
memberPipSharp18-Aug-09 1:36 
GeneralRe: Associations not Working for currently associated files Pin
LimitedAtonement3-Feb-10 11:03
memberLimitedAtonement3-Feb-10 11:03 
GeneralRe: Associations not Working for currently associated files Pin
TheToid12-May-10 14:51
memberTheToid12-May-10 14:51 
QuestionI want to use your code in my app. Can I ? Pin
IQ719-Jun-09 4:27
memberIQ719-Jun-09 4:27 
QuestionHow to run this application Pin
KapilDesai17-May-09 23:42
memberKapilDesai17-May-09 23:42 
QuestionHow to use it for an application Pin
Michael Egan9-Apr-09 23:46
memberMichael Egan9-Apr-09 23:46 
Generaldoes not work under VISTA Pin
viewon013-Apr-08 11:59
memberviewon013-Apr-08 11:59 
GeneralFilenames with spaces Pin
pucis8324-Feb-08 14:15
memberpucis8324-Feb-08 14:15 
QuestionHow to get icon from ProgramAssociationInfo.DefaultIcon Pin
hoanhtuan8-Jan-08 22:34
memberhoanhtuan8-Jan-08 22:34 
AnswerRe: How to get icon from ProgramAssociationInfo.DefaultIcon Pin
Aschratt31-Jan-08 8:51
memberAschratt31-Jan-08 8:51 
GeneralDefaultIcon Bug [modified] Pin
Aschratt2-Dec-07 0:16
memberAschratt2-Dec-07 0:16 
GeneralRe: DefaultIcon Bug Pin
zackflame15-Jun-10 5:48
memberzackflame15-Jun-10 5:48 
GeneralAccess to registry key denied Pin
kamal2003us2-Jul-07 0:49
memberkamal2003us2-Jul-07 0:49 
GeneralRe: Access to registry key denied Pin
Brendan Grant2-Jul-07 6:24
memberBrendan Grant2-Jul-07 6:24 
GeneralRe: Access to registry key denied Pin
yf0129-Dec-07 21:39
memberyf0129-Dec-07 21:39 
GeneralMay not need admin access under Vista Pin
The_Mega_ZZTer16-Mar-07 15:42
memberThe_Mega_ZZTer16-Mar-07 15:42 
QuestionGreat stuff! Can I use it? Pin
Richard Brightwell8-Mar-07 9:27
memberRichard Brightwell8-Mar-07 9:27 
AnswerRe: Great stuff! Can I use it? Pin
Brendan Grant12-Mar-07 7:18
memberBrendan Grant12-Mar-07 7:18 
GeneralBrief version [modified] Pin
MatthiasBiel21-Feb-07 21:41
memberMatthiasBiel21-Feb-07 21:41 
GeneralRe: Brief version Pin
Testo28ß519-Jun-08 11:45
memberTesto28ß519-Jun-08 11:45 
GeneralRe: Brief version Pin
kasparovthe218-Feb-09 7:33
memberkasparovthe218-Feb-09 7:33 
GeneralRe: Brief version Pin
spamblot13-Mar-09 5:51
memberspamblot13-Mar-09 5:51 
GeneralRe: Brief version Pin
Wayne Walter6-May-10 9:26
memberWayne Walter6-May-10 9:26 
GeneralHey Brendan Pin
Tall Dude9-Jan-07 21:20
memberTall Dude9-Jan-07 21:20 
GeneralRe: Hey Brendan Pin
Brendan Grant10-Jan-07 5:59
memberBrendan Grant10-Jan-07 5:59 
GeneralRe: Hey Brendan Pin
Tall Dude10-Jan-07 9:51
memberTall Dude10-Jan-07 9:51 
GeneralRe: Hey Brendan Pin
Brendan Grant21-Mar-07 18:45
memberBrendan Grant21-Mar-07 18:45 
GeneralRe: Hey Brendan Pin
Tall Dude22-Mar-07 14:04
memberTall Dude22-Mar-07 14:04 
GeneralNice code! Pin
Rynus9-Jan-07 4:41
memberRynus9-Jan-07 4:41 
GeneralRe: Nice code! Pin
Brendan Grant10-Jan-07 5:57
memberBrendan Grant10-Jan-07 5:57 
GeneralGreat [modified] Pin
DrJaymz5-Jan-07 7:38
memberDrJaymz5-Jan-07 7:38 
GeneralRe: Great Pin
Brendan Grant10-Jan-07 5:55
memberBrendan Grant10-Jan-07 5:55 
GeneralRe: Great Pin
DrJaymz10-Jan-07 6:59
memberDrJaymz10-Jan-07 6:59 
GeneralRe: Great Pin
Brendan Grant10-Jan-07 11:19
memberBrendan Grant10-Jan-07 11:19 
GeneralRe: Great Pin
Diamonddrake29-May-09 10:46
memberDiamonddrake29-May-09 10:46 

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.

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