Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

SHOpenWithDialog (shell32)
 
.
Summary
Shows the Windows File association API

C# Signature:

    [DllImport("shell32.dll", EntryPoint = "SHOpenWithDialog", CharSet = CharSet.Unicode)]
    private static extern int SHOpenWithDialog(IntPtr hWndParent, ref tagOPENASINFO oOAI);

User-Defined Types:

    // http://msdn.microsoft.com/en-us/library/windows/desktop/bb773363(v=vs.85).aspx
    private struct tagOPENASINFO
    {
        [MarshalAs(UnmanagedType.LPWStr)]
        public string cszFile;

        [MarshalAs(UnmanagedType.LPWStr)]
        public string cszClass;

        [MarshalAs(UnmanagedType.I4)]
        public tagOPEN_AS_INFO_FLAGS oaifInFlags;
    }

    [Flags]
    private enum tagOPEN_AS_INFO_FLAGS
    {
           OAIF_ALLOW_REGISTRATION =  0x00000001,   // Show "Always" checkbox
           OAIF_REGISTER_EXT =    0x00000002,   // Perform registration when user hits OK
           OAIF_EXEC =        0x00000004,   // Exec file after registering
           OAIF_FORCE_REGISTRATION =  0x00000008,   // Force the checkbox to be registration
           OAIF_HIDE_REGISTRATION =   0x00000020,   // Vista+: Hide the "always use this file" checkbox
           OAIF_URL_PROTOCOL =    0x00000040,   // Vista+: cszFile is actually a URI scheme; show handlers for that scheme
           OAIF_FILE_IS_URI =     0x00000080    // Win8+: The location pointed to by the pcszFile parameter is given as a URI
    }

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

Requires Windows Vista or later; on earlier platforms, use rundll32 to run OpenAs

Tips & Tricks:

Please add some!

Sample Code:

    public static void DoOpenFileWith(string sFilename)
    {
        if (Environment.OSVersion.Version.Major > 5)
        {
        IntPtr hwndParent = IntPtr.Zero;
        if (null != MyAppForm) hwndParent = MyAppForm.Handle;

        tagOPENASINFO oOAI = new tagOPENASINFO();
        oOAI.cszFile = sFilename;
        oOAI.cszClass = String.Empty;
        oOAI.oaifInFlags = tagOPEN_AS_INFO_FLAGS.OAIF_ALLOW_REGISTRATION | tagOPEN_AS_INFO_FLAGS.OAIF_EXEC;
        SHOpenWithDialog(hwndParent, ref oOAI);
        }
        else
        {
        using (Process.Start("rundll32", "shell32.dll,OpenAs_RunDLL " + sFilename)) { }
        }
    }

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions