Click here to Skip to main content
13,287,315 members (53,141 online)
Click here to Skip to main content
Add your own
alternative version


74 bookmarked
Posted 10 Nov 2009

C# FileAssociation Class

, 14 Nov 2009
Rate this:
Please Sign up or sign in to vote.
Allows you to quickly create, edit, and delete file associations in your registry. A file extension's default icon, description, executable application, etc.


My small library here allows you to set File Associations. File Associations are a group of multiple keys in your registry that set what program a file type opens with, the description of the file type that's shown in the file properties dialog, the icon that's shown on a file type, and even what programs are displayed in the dialog that opens when Windows can't find the program that opens with a file type (Open with list).


I've been using C# for about a year now... I previously ran into the problem of File Associations when I was working on a big word processor project with never-before-seen features. So I did some research and figured out how to do this.

Using the Code

The download at the top contains a CS file and a DLL file. The CS file contains my library's code so you can learn how it all works, and the DLL is for adding a reference to in your programs. The code block below shows how you use the library after referencing it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AF_Lib.IO.Associations;

public class AFFileAssociations_Example
    public static void Main()
        // Initializes a new AF_FileAssociator to associate the .ABC file extension.
        AF_FileAssociator assoc = new AF_FileAssociator(".abc");

        // Creates a new file association for the .ABC file extension. 
        // Data is overwritten if it already exists.
            "My application's file association",
            new ProgramIcon(@"C:\Program Files\My_App\icon.ico"),
            new ExecApplication(@"C:\Program Files\My_App\myapp.exe"),
            new OpenWithList(new string[] { "My_App" }));

        // Gets each piece of association info individually, all as strings.
        string id = assoc.ID;
        string description = assoc.Description;
        string icon = assoc.DefaultIcon.IconPath;
        string execApp = assoc.Executable.Path;
        string[] openWithList = assoc.OpenWith.List;

        // Sets each piece of association info individually.
        ProgramIcon newDefIcon = new ProgramIcon(@"C:\Program Files\My_App\icon2.ico");
        ExecApplication newExecApp = 
		new ExecApplication(@"C:\Program Files\My_App\myapp2.exe");
        OpenWithList newOpenWith = new OpenWithList(new string[] { "myapp2.exe" });

        assoc.ID = "My_App_2";
        assoc.Description = "My application's file association #2";
        assoc.DefaultIcon = newDefIcon;
        assoc.Executable = newExecApp;
        assoc.OpenWith = newOpenWith;

        // Gets the extension of the associator that was set when initializing it.
        string extension = assoc.Extension;

        // Deletes any keys that were associated with the .ABC file extension.

Points of Interest

This library should be very helpful. The attachment on this project contains my code file, and the DLL to reference it. Please leave any questions or comments below. Thanks for reading my article!


  • 10th November, 2009: Initial post
  • 11th November, 2009: Updated source code


This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


About the Author

Aidan Follestad
Software Developer Ricco Solutions
United States United States

You may also be interested in...

Comments and Discussions

GeneralSupport file paths with spaces Pin
Dmitry S. Putyatov5-Jan-17 7:54
memberDmitry S. Putyatov5-Jan-17 7:54 
Questionargs does not pass correctly Pin
Member 1108003724-Feb-15 1:06
memberMember 1108003724-Feb-15 1:06 
Questioncan u help me? Pin
Le@rner16-Apr-14 21:14
memberLe@rner16-Apr-14 21:14 
QuestionHow to make file de-association made by other application? Pin
Stan Huang19-Feb-14 21:30
memberStan Huang19-Feb-14 21:30 
AnswerRe: How to make file de-association made by other application? Pin
Stan Huang19-Feb-14 23:28
memberStan Huang19-Feb-14 23:28 
QuestionIt think it would be nice if you can update your article to ensure nobody will continue to use it. Pin
Eric Ouellet16-Oct-13 7:01
memberEric Ouellet16-Oct-13 7:01 
QuestionAssociation file path??? Pin
KM Perumal8-Nov-12 8:56
groupKM Perumal8-Nov-12 8:56 
GeneralDeprecated Pin
Aidan Follestad8-Jul-10 21:34
memberAidan Follestad8-Jul-10 21:34 
NewsA compacter way in Pin
Michael Bakker6-Jan-10 10:18
memberMichael Bakker6-Jan-10 10:18 
GeneralGumby Question here: Adding DLL as a reference Pin
chris_ciptech8-Dec-09 17:07
memberchris_ciptech8-Dec-09 17:07 
GeneralRe: Gumby Question here: Adding DLL as a reference Pin
nethsu6-Oct-10 0:03
membernethsu6-Oct-10 0:03 
Aidan F11-Nov-09 18:00
memberAidan F11-Nov-09 18:00 
GeneralLooks good! Pin
Ant210011-Nov-09 11:02
memberAnt210011-Nov-09 11:02 
GeneralRe: Looks good! Pin
Aidan F11-Nov-09 16:02
memberAidan F11-Nov-09 16:02 
GeneralOpenWithList Pin
Johnny J.10-Nov-09 21:51
memberJohnny J.10-Nov-09 21:51 
GeneralRe: OpenWithList Pin
Johnny J.10-Nov-09 22:01
memberJohnny J.10-Nov-09 22:01 
GeneralRe: OpenWithList Pin
Aidan F11-Nov-09 16:08
memberAidan F11-Nov-09 16:08 
GeneralRe: OpenWithList Pin
Aidan F11-Nov-09 16:03
memberAidan F11-Nov-09 16:03 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 14 Nov 2009
Article Copyright 2009 by Aidan Follestad
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid