Share via

Change file association for .PDF programmatically

Santosh Menon 20 Reputation points
2026-02-10T13:13:53.8633333+00:00

Screenshot 2026-01-20 140106

i want to show user this dialog so that my user can select my application to make it default app for pdf, plz help. my application is in c#

Windows development | Windows API - Win32
0 comments No comments

Answer accepted by question author

Castorix31 91,876 Reputation points
2026-02-10T14:52:52.84+00:00

This works on Windows 10 :

SHELLEXECUTEINFO sei = new SHELLEXECUTEINFO();
sei.cbSize = (uint)Marshal.SizeOf(sei);                    
sei.lpVerb = "openas";
sei.lpFile = @"E:\test.pdf";
sei.fMask = SEE_MASK_INVOKEIDLIST;                   
if (!ShellExecuteEx(ref sei))
{
    int nErr = Marshal.GetLastWin32Error();
}

with :

       [DllImport("Shell32.dll", SetLastError = false, CharSet = CharSet.Unicode)]
       public static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);

       [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
       public struct SHELLEXECUTEINFO
       {
           public uint cbSize;
           public uint fMask;
           public IntPtr hwnd;
           [MarshalAs(UnmanagedType.LPWStr)]
           public string lpVerb;
           [MarshalAs(UnmanagedType.LPWStr)]
           public string lpFile;
           [MarshalAs(UnmanagedType.LPWStr)]
           public string lpParameters;
           [MarshalAs(UnmanagedType.LPWStr)]
           public string lpDirectory;
           public int nShow;
           public IntPtr hInstApp;
           public IntPtr lpIDList;
           [MarshalAs(UnmanagedType.LPWStr)]
           public string lpClass;
           public IntPtr hkeyClass;
           public uint dwHotKey;
           public IntPtr hIcon;
           public IntPtr hProcess;
       }

       public const int SEE_MASK_INVOKEIDLIST = 0x0000000C;

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.