Programmatically opening the 'Default Programs' window

Windows Vista comes with a new feature called Default Programs (which replaces the Set Program Access and Default and File Types feature from Windows XP). This cool new feature provides a user friendly way of changing program defaults, such as file and protocol associations and AutoPlay settings.

'Default Programs' feature in Windows Vista

The recommendations going forward is that on Windows Vista or higher, applications use the new UI to allow users to customize file associations for an application instead of providing their own. This provides a consistent look and feel for every application and provides a single place for users to change these settings.

Unfortunately, to programmatically interact with this feature is not as straight forward as you might think from managed code if you've never done COM interop. The docs for these APIs leave a lot to be desired (why common return values from COM methods aren't called out on MSDN - I don't know) and there are, at the time of writing, no samples for calling them.

To save you most of the trouble, I've gone ahead and written a small wrapper around the IApplicationAssociationRegistrationUI::LaunchAdvancedAssociationUI method, which displays the file association window for a particular application.

To use this method, simply take a reference to the below attached project and write something like:

static void Main()
{
    System.Windows.DefaultApplications.ShowAssociationsWindow("Internet Explorer");
}

The above example, will open the Set associations for a program window for Internet Explorer:

'Set associations for a program' window

The name of the application to pass to DefaultApplications.ShowAssociationsWindow comes from one of the registry values under HKEY_LOCAL_MACHINE\Software\RegisteredApplications. Information on this key can be found at Registering an Application for Use with Default Programs.

For now, download the wrapper (System.Windows.DefaultApplications.zip) and tell me what you think.

For additional information on how to make your application play nicely with Default Programs, see http://msdn.microsoft.com/en-us/library/bb776873.aspx

Published Wednesday, December 03, 2008 7:00 AM by David Kean

Comments

Friday, January 16, 2009 1:09 AM by Jumpy

# re: Programmatically opening the 'Default Programs' window

Thank you so much. I have been waiting for something like this for ages.

Shame on Microsoft to make it so hard. Grrrr!

Saturday, April 18, 2009 5:28 AM by Fred

# re: Programmatically opening the 'Default Programs' window

What license is this code released under? Can I include it in a (New) BSD licensed project if I add your name to the AUTHORS file?

Wednesday, June 17, 2009 7:16 AM by robert_d

# re: Programmatically opening the 'Default Programs' window

Thanks Dave for this code.

Can you explain why Microsoft didn't provide similar wrapper library for all Win32 API public methods?

This is ridiculous to go through all those hoops in order to use one Win32 API method in C#.

Wednesday, June 17, 2009 10:51 AM by robert_d

# re: Programmatically opening the 'Default Programs' window

I am getting following error when I try to run a function that I added to your code

Unhandled Exception: System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {3668A50D-EFB3-4C30-9A8C-C34C3632FC60} failed due to the following error: 80040154.

3668A50D-EFB3-4C30-9A8C-C34C3632FC60 is a random Guid number that is Guid attribute of my ApplicationAssociationRegistration class (similar to yours ApplicationAssociationRegistrationUI class) - I am trying to wrap IApplicationAssociationRegistration::SetAppAsDefaultAll method the same way you wrapped LaunchAdvancedAssociationUI method.

How did you come up with Guid attributes for your classes?

BTW your code runs well on my PC.

Wednesday, June 17, 2009 12:37 PM by robert_d

# re: Programmatically opening the 'Default Programs' window

OK I found correct GUIDs in ShObjIdl.h file on my PC.

But now I've got this error:

System.AccessViolationException was unhandled

 Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

Again, this jumping through hoops is ridiculous,

I bet that Apple treats its developers much better ;-)

Thursday, July 30, 2009 4:56 AM by BenBtg

# re: Programmatically opening the 'Default Programs' window

I having trouble getting this to work in Windows 7. Has this been changed? Anybody managed to get the associations dialog displayed in Windows 7?

I just tried this on Windows 7 RTM and it seems to be working - what troubles are you having?