Example #1
0
    public static DialogResult ShowFolderDialog(this OpenFileDialog ofd, IntPtr hWndOwner)
    {
        if (Environment.OSVersion.Version.Major < 6)
        {
            throw new NotSupportedException();
        }
        var oldVal = ofd.CheckFileExists;

        if (oldVal == true)
        {
            ofd.CheckFileExists = false;
        }

        //System.Windows.Forms.FileDialog::RunDialogVista(IntPtr hWndOwner)
        /*IFileDialog*/ var dialog = ofd.CreateVistaDialog();

        ofd.OnBeforeVistaDialog(dialog);
        /*VistaDialogEvents*/ var events = new_VistaDialogEvents(ofd);

        dialog.SetOptions(ofd.GetOptions() | 32U); /*FOS_PICKFOLDERS*/
        dialog.Advise(events, out uint id);
        try {
            return(Show(dialog, hWndOwner));
        } finally {
            dialog.Unadvise(id);
            GC.KeepAlive(events);
            if (oldVal == true)
            {
                ofd.CheckFileExists = true;
            }
        }
    }