Click here to Skip to main content
Licence 
First Posted 5 Jan 2007
Views 128,464
Downloads 2,866
Bookmarked 115 times

System File Association

By | 16 Mar 2007 | Article
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

About the Author

Brendan Grant

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
QuestionDoesnt work with Win7 PinmemberHrizip2:50 5 Dec '11  
Question%1 argument PinmemberBence Sebők8:43 7 Jun '11  
AnswerRe: %1 argument PinmemberDimitris Vassiliades10:52 7 Jun '11  
GeneralMy vote of 5 PinmemberMario Majcica11:26 19 May '11  
GeneralGreat article Pinmemberjatinsehgal862:51 18 May '11  
AnswerProgramIcon bug fix! PinmemberWaldo Bronchart2:27 1 Mar '11  
GeneralRe: ProgramIcon bug fix! Pinmemberikkentim10:35 31 Jan '12  
GeneralSome notes about file extensions on windows PinmemberTheToid13:47 12 May '10  
GeneralGreat post, one comment PinmemberLimitedAtonement10:02 3 Feb '10  
Generalthanks for the code Pinmemberwas830918:02 17 Sep '09  
GeneralAssociations not Working for currently associated files PinmemberPipSharp0:36 18 Aug '09  
GeneralRe: Associations not Working for currently associated files PinmemberLimitedAtonement10:03 3 Feb '10  
GeneralRe: Associations not Working for currently associated files PinmemberTheToid13:51 12 May '10  
QuestionI want to use your code in my app. Can I ? PinmemberIQ73:27 19 Jun '09  
QuestionHow to run this application PinmemberKapilDesai22:42 17 May '09  
QuestionHow to use it for an application PinmemberMichael Egan22:46 9 Apr '09  
Generaldoes not work under VISTA Pinmemberviewon0110:59 3 Apr '08  
GeneralFilenames with spaces Pinmemberpucis8313:15 24 Feb '08  
QuestionHow to get icon from ProgramAssociationInfo.DefaultIcon Pinmemberhoanhtuan21:34 8 Jan '08  
Brendan,
 
How to get the icon from path string of ProgramAssociationInfo.DefaultIcon ?
 
I'm writing a explorer type program, and will need to display icon for each file type.
 
You done a great job, Rose | [Rose] , thanks much.
 
Anh Tuan
AnswerRe: How to get icon from ProgramAssociationInfo.DefaultIcon PinmemberAschratt7:51 31 Jan '08  
GeneralDefaultIcon Bug [modified] PinmemberAschratt23:16 1 Dec '07  
GeneralRe: DefaultIcon Bug Pinmemberzackflame4:48 15 Jun '10  
GeneralAccess to registry key denied Pinmemberkamal2003us23:49 1 Jul '07  
GeneralRe: Access to registry key denied PinmemberBrendan Grant5:24 2 Jul '07  
GeneralRe: Access to registry key denied Pinmemberyf01220:39 9 Dec '07  

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
Web03 | 2.5.120529.1 | Last Updated 16 Mar 2007
Article Copyright 2007 by Brendan Grant
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid