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");
fai.ContentType = "application/myfile";
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
(
"My Program's File Type",
new ProgramVerb
(
"Open",
@"C:\SomePath\MyApp.exe %1"
)
);
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.