Exemplo n.º 1
0
        public bool ShowDialog(Window owner)
        {
            var instanceNonPublic = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

            var vistaDialog = ofd.GetType().GetMethod("CreateVistaDialog", instanceNonPublic).Invoke(ofd, null);

            ofd.GetType().GetMethod("OnBeforeVistaDialog", instanceNonPublic).Invoke(ofd, new[] { vistaDialog });

            var FileDialogNative = Assembly.GetAssembly(typeof(Forms.OpenFileDialog)).GetType("System.Windows.Forms.FileDialogNative");
            var IFileDialog      = FileDialogNative.GetNestedType("IFileDialog", BindingFlags.NonPublic);

            var options = (uint)typeof(Forms.FileDialog).GetMethod("GetOptions", instanceNonPublic).Invoke(ofd, null);

            options |= (uint)FileDialogNative.GetNestedType("FOS", BindingFlags.NonPublic).GetField("FOS_PICKFOLDERS").GetValue(null);
            IFileDialog.GetMethod("SetOptions", instanceNonPublic).Invoke(vistaDialog, new object[] { options });

            var VistaDialogEvents = typeof(Forms.FileDialog).GetNestedType("VistaDialogEvents", BindingFlags.NonPublic);
            var events            = Activator.CreateInstance(VistaDialogEvents, ofd);

            var adviseParams = new object[] { events, (uint)0 };

            IFileDialog.GetMethod("Advise", instanceNonPublic).Invoke(vistaDialog, adviseParams);

            var adviseResult = (uint)adviseParams[1];

            bool showResult;

            try
            {
                var handle = owner == null ? IntPtr.Zero : new System.Windows.Interop.WindowInteropHelper(owner).Handle;
                showResult = 0 == (int)IFileDialog.GetMethod("Show", instanceNonPublic).Invoke(vistaDialog, new object[] { handle });
            }
            finally
            {
                IFileDialog.GetMethod("Unadvise", instanceNonPublic).Invoke(vistaDialog, new object[] { adviseResult });
                GC.KeepAlive(events);
            }

            return(showResult);
        }
Exemplo n.º 2
0
        public static string[] Show(Form parent)
        {
            if (Environment.OSVersion.Version.Major >= 6){ // vista and newer
                try{
                    OpenFileDialog dialog = new OpenFileDialog{
                        Filter = Lang.Get["DialogFilterFolders"]+FilterFolders,
                        CheckFileExists = false,
                        CheckPathExists = true,
                        DereferenceLinks = true,
                        AddExtension = false,
                        Multiselect = true,
                        AutoUpgradeEnabled = true
                    };

                    Assembly assembly = LoadAssembly("System.Windows.Forms");
                    Type ifdType = assembly.GetType("System.Windows.Forms.FileDialogNative").GetNestedType("IFileDialog", BindingFlags.NonPublic);
                    Type fdType = typeof(FileDialog);
                    Type dialogType = dialog.GetType();

                    const BindingFlags flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

                    var vistaDialog = dialogType.GetMethod("CreateVistaDialog", flags).Invoke(dialog, new object[0]);
                    dialogType.GetMethod("OnBeforeVistaDialog", flags).Invoke(dialog, new object[]{ vistaDialog });

                    uint opts = (uint)fdType.GetMethod("GetOptions", flags).Invoke(dialog, new object[0]);
                    ifdType.GetMethod("SetOptions", flags).Invoke(vistaDialog, new object[]{ opts | 32u }); // 32u = FOS_PICKFOLDERS

                    object events = fdType.GetNestedType("VistaDialogEvents", flags).GetConstructors()[0].Invoke(new object[]{ dialog });
                    object[] parameters = { events, (uint)0 };
                    ifdType.GetMethod("Advise").Invoke(vistaDialog, parameters);
                    uint id = (uint)parameters[1];

                    try{
                        ifdType.GetMethod("Show").Invoke(vistaDialog, new object[]{ parent.Handle });
                    }finally{
                        ifdType.GetMethod("Unadvise").Invoke(vistaDialog, new object[]{ id });
                        GC.KeepAlive(events);
                    }

                    return dialog.FileNames;
                }catch(Exception e){
                    Debug.Write(e.ToString());
                }
            }

            var oldDialog = new FolderBrowserDialog{ ShowNewFolderButton = false };
            return oldDialog.ShowDialog(parent) == DialogResult.OK ? new string[]{ oldDialog.SelectedPath } : new string[0];
        }