Click here to Skip to main content
15,888,351 members
Articles / Programming Languages / C#
Article

System File Association

Rate me:
Please Sign up or sign in to vote.
4.90/5 (44 votes)
16 Mar 20072 min read 344K   12.3K   140   52
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.

C#
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.

C#
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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Brief version Pin
n0pt3x18-Feb-09 6:33
n0pt3x18-Feb-09 6:33 
GeneralRe: Brief version Pin
Zot Williams13-Mar-09 4:51
Zot Williams13-Mar-09 4:51 
GeneralRe: Brief version Pin
Wayne Walter6-May-10 8:26
Wayne Walter6-May-10 8:26 
GeneralHey Brendan Pin
Tall Dude9-Jan-07 20:20
Tall Dude9-Jan-07 20:20 
GeneralRe: Hey Brendan Pin
Brendan Grant10-Jan-07 4:59
Brendan Grant10-Jan-07 4:59 
GeneralRe: Hey Brendan Pin
Tall Dude10-Jan-07 8:51
Tall Dude10-Jan-07 8:51 
GeneralRe: Hey Brendan Pin
Brendan Grant21-Mar-07 17:45
Brendan Grant21-Mar-07 17:45 
GeneralRe: Hey Brendan Pin
Tall Dude22-Mar-07 13:04
Tall Dude22-Mar-07 13:04 
Brendan,

I had a little while today to try out the VB example solution.

Problem 1, (for me), VB Express will not open and run the program
in debug mode because it does not use the VB express partial classes
setup. Also, I, being an amatuer, was not used to working with multiple
projects in one solution Frown | :(

It would run by directly clicking on the exe in the solution from
Windows Explorer.

(I did restructure the code to run in the VB Express debugger.)

Problem 2: At one point I was able to crash the program by using the top
'update' button with the 'persistant handler' set to blank.

Problem 3: Having not read up too much on the subject of how Windows
handles file associations, the program was not intuative to me.
(Me dumb)

When trying to use the 'new extension' option, I had no idea what to put in the
other fields. Wheather thay could be made up, or had to equal some real value.
Also, the 'persistanthandler' seems to be a GUID, which I know nothing about.
Where would I look to find the GUID for my VB program?

A lot of the 'program association' info at the bottom, ended in
'/n /dde', which I have no idea what that means.
I figured out for my program to gets it's file argument I had to use "%1"
at the end, which did work.

The 'add' and 'remove' at the bottom, let you add/remove things like 'open'
(called a verb?) The open at bottom is the only one I worked with.

Problem 4: Some of the updates would not show up until the program was
closed and re-opened. Can't remember which ones.

Problem 5: Pointing to a new exe (VB program exe) for the icon source did not
change the icon source. (Is that just an info only field?)

More comments when I get more time to tinker.
GeneralNice code! Pin
Rynus_Rein9-Jan-07 3:41
Rynus_Rein9-Jan-07 3:41 
GeneralRe: Nice code! Pin
Brendan Grant10-Jan-07 4:57
Brendan Grant10-Jan-07 4:57 
GeneralGreat [modified] Pin
DrJaymz5-Jan-07 6:38
DrJaymz5-Jan-07 6:38 
GeneralRe: Great Pin
Brendan Grant10-Jan-07 4:55
Brendan Grant10-Jan-07 4:55 
GeneralRe: Great Pin
DrJaymz10-Jan-07 5:59
DrJaymz10-Jan-07 5:59 
GeneralRe: Great Pin
Brendan Grant10-Jan-07 10:19
Brendan Grant10-Jan-07 10:19 
GeneralRe: Great Pin
Diamonddrake29-May-09 9:46
Diamonddrake29-May-09 9:46 

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.