Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show "Set program associations" window in Windows 8/8.1?

I want to show Set program associations windows [edit: for my program] like Chrome do when it run first time after install, like this:

                        enter image description here

It have:

  • Name of my program
  • File types I want to set default program (more than one)

How to do it by c# (or win32 c++)?

Edit
I read some articles about modify registry (by .reg file or by code work around modify registry) to set default program. It seem only work in Windows XP/7 or lower, not 8/8.1 as Microsoft use hash to protect these registry.

like image 288
Thanh Nguyen Avatar asked Apr 24 '15 12:04

Thanh Nguyen


1 Answers

This is all explained in the official documentation here: Guidelines for File Associations and Default Programs

To launch this Windows-provided UI, you can use the IApplicationAssociationRegistrationUI interface.

Here is a sample console app that demonstrate this for a fictional "MyApp" application:

class Program
{
    static void Main(string[] args)
    {
        IApplicationAssociationRegistrationUI app = (IApplicationAssociationRegistrationUI)new ApplicationAssociationRegistrationUI();
        int hr = app.LaunchAdvancedAssociationUI("MyApp");
        Exception error = Marshal.GetExceptionForHR(hr);
        if (error != null)
        {
            Console.WriteLine("Error: " + error.Message);
        }
    }
}

[Guid("1f76a169-f994-40ac-8fc8-0959e8874710")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IApplicationAssociationRegistrationUI
{
    [PreserveSig]
    int LaunchAdvancedAssociationUI([MarshalAs(UnmanagedType.LPWStr)] string pszAppRegName);
}

[ComImport]
[Guid("1968106d-f3b5-44cf-890e-116fcb9ecef1")]
public class ApplicationAssociationRegistrationUI
{
}

And this is not finished :-) This only works if the registry is properly setup for this "MyApp" application, which is kinda the difficult part. Here are the simplest steps needed for this to work:

1) create a fictional "MyAppHTML" progid in HKCR, like this:

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\MyAppHTML]
@="MyApp HTML Document"

[HKEY_CLASSES_ROOT\MyAppHTML\Application]
"ApplicationCompany"="Fictional Software Inc."

[HKEY_CLASSES_ROOT\MyAppHTML\shell]
@="open"

[HKEY_CLASSES_ROOT\MyAppHTML\shell\open]

[HKEY_CLASSES_ROOT\MyAppHTML\shell\open\command]
@="\"C:\\the app path\\testassoc.exe\""

2) declare a fictional "MyApp" application (I suggest HKCU, but it could be HKLM), from a fictional "FictionalSoftware" company, like this, for example with two file associations:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\FictionalSoftware]

[HKEY_CURRENT_USER\Software\FictionalSoftware\MyApp]

[HKEY_CURRENT_USER\Software\FictionalSoftware\MyApp\Capabilities]
"ApplicationDescription"="My Fictional Application"

[HKEY_CURRENT_USER\Software\FictionalSoftware\MyApp\Capabilities\FileAssociations]
".htm"="MyAppHTML"
".html"="MyAppHTML"

3) register this application to Windows (again, HKCU could be replaced by HKLM), like this:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\RegisteredApplications]
"MyApp"="Software\\FictionalSoftware\\MyApp\\Capabilities"

If you get errors when running the sample app, then you probably messed up the registry layout. If you got it ok, then you should see something like this:

enter image description here

like image 191
Simon Mourier Avatar answered Nov 03 '22 01:11

Simon Mourier