Location>code7788 >text

C# Call Win10/11 File Association Dialog

Popularity:98 ℃/2025-04-02 15:31:38

Method 1: Call the unpublished interface IOpenWithLauncher

Adobe Acrobat should be the unpublished interface method called
[ComImport]
[InterfaceType()]
[Guid("6A283FE2-ECFA-4599-91C4-E80957137B26")]
interface IOpenWithLauncher
{
    [PreserveSig]
    int Launch(IntPtr hWndParent,
         [MarshalAs()] string lpszPath,
         IMMERSIVE_OPENWITH flags);
}

[Flags]
enum IMMERSIVE_OPENWITH
{
    NONE = 0,
    OVERRIDE = 0x1,
    DONOT_EXEC = 0x4,
    PROTOCOL = 0x8,
    URL = 0x10,
    USEPOSITION = 0x20,
    DONOT_SETDEFAULT = 0x40,
    ACTION = 0x80,
    ALLOW_EXECDEFAULT = 0x100,
    NONEDP_TO_EDP = 0x200,
    EDP_TO_NONEDP = 0x400,
    CALLING_IN_APP = 0x800,
};

public static void ShowSetAssocDialog(string extension)
{
    var CLSID_ExecuteUnknown = new Guid("{E44E9428-BDBC-4987-A099-40DC8FD255E7}");
    var obj = ((CLSID_ExecuteUnknown));
    if (obj is IOpenWithLauncher launcher)
    {
        (, extension, IMMERSIVE_OPENWITH.DONOT_EXEC);
        (launcher);
    }
}

Method 2: Open method by simulated clicking on properties dialog box "Change"

This method comes from my self-question two years ago, and the code quotes"C# Window Process Message Processing WndProc"Attach to other window helper classes in

This method used by Bandizip
[DllImport("", CharSet = )]
 static extern bool SHObjectProperties(IntPtr hWnd, SHOP shopObjectType, string pszObjectName, string pszPropertyPage);

 enum SHOP
 {
     PRINTERNAME = 1,
     FILEPATH = 2,
     VOLUMEGUID = 4
 }

 [DllImport("")]
 static extern int GetCurrentProcessId();

 [DllImport("")]
 static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

 [DllImport("", CharSet = )]
 static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

 [DllImport("", CharSet = )]
 static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

 [DllImport("")]
 static extern bool SetForegroundWindow(IntPtr hWnd);

 [DllImport("")]
 static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);

 delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);

 public static void ShowSetAssocDialog(string extension)
 {
     string fileName = ((), extension);
     string filePath = ((), fileName);
     fileName = (fileName);
     (filePath, ); // Create temporary files
     var frame = new DispatcherFrame();
     int pid = GetCurrentProcessId();
     SHObjectProperties(, , filePath, null); // Display the properties dialog box
     While (true)
     {
         bool found = !EnumWindows((hWnd, lParam) => // Enum window
         {
             GetWindowThreadProcessId(hWnd, out int id);
             if (id == pid) // Compare process id
             {
                 const int MAX_PATH = 260;
                 var sb = new StringBuilder(MAX_PATH);
                 GetClassName(hWnd, sb,);
                 if (() == "#32770") // Dialog box class name
                 {
                     GetWindowText(hWnd, sb, );
                     if (().Contains(fileName)) // Does the dialog box title contain a file name
                     {
                         SetWindowPos(hWnd, , 0, 0, 0, 0, 0, );// Hide properties dialog box
                         (hWnd, (ref Message m) =>
                         {
                             const int PSM_CHANGED = 0x400 + 104;
                             if ( == PSM_CHANGED)// Monitor property table page changes
                             {
                                  = false;
                                 PostMessage(hWnd, , 0, 0); // Equivalent EndDialog(hWnd, 0)
                             }
                             return false;
                         });
                         SetForegroundWindow(hWnd);
                         ("%C");// ALT + C shortcut keys
                         return false;
                     }
                 }
             }
             return true;
         }, );
         if (found) break;
     }
     (filePath); // Delete temporary files
     (frame);
 }

Related information

How to call the "Open With" dialog used to associate file formats in Windows 10 or 11?

/en-us/windows/win32/controls/psm-changed