Introduction
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).
Background
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()
{
AF_FileAssociator assoc = new AF_FileAssociator(".abc");
assoc.Create("My_App",
"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" }));
string id = assoc.ID;
string description = assoc.Description;
string icon = assoc.DefaultIcon.IconPath;
string execApp = assoc.Executable.Path;
string[] openWithList = assoc.OpenWith.List;
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;
string extension = assoc.Extension;
assoc.Delete();
}
}
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!
History
- 10th November, 2009: Initial post
- 11th November, 2009: Updated source code