public System.Windows.Forms.DialogResult ShowDialog(IntPtr owner) { var dlg = new FileOpenDialogInternal() as IFileOpenDialog; try { dlg.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM); IShellItem item; if (!string.IsNullOrEmpty(this.Path)) { IntPtr idl; uint atts = 0; if (NativeMethods.SHILCreateFromPath(this.Path, out idl, ref atts) == 0) { if (NativeMethods.SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out item) == 0) { dlg.SetFolder(item); } } } if (!string.IsNullOrEmpty(this.Title)) { dlg.SetTitle(this.Title); } var hr = dlg.Show(owner); if (hr.Equals(NativeMethods.ERROR_CANCELLED)) { return(System.Windows.Forms.DialogResult.Cancel); } if (!hr.Equals(0)) { return(System.Windows.Forms.DialogResult.Abort); } dlg.GetResult(out item); string outputPath; item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out outputPath); this.Path = outputPath; return(System.Windows.Forms.DialogResult.OK); } finally { Marshal.FinalReleaseComObject(dlg); } }
public DialogResult ShowDialog(IntPtr owner) { var dialog = new FileOpenDialogInternal() as IFileOpenDialog; try { IShellItem item; string selectedPath; dialog.SetOptions(_FILEOPENDIALOGOPTIONS.FOS_PICKFOLDERS | _FILEOPENDIALOGOPTIONS.FOS_FORCEFILESYSTEM); if (!string.IsNullOrEmpty(SelectedPath)) { IntPtr idl = IntPtr.Zero; // path の intptr uint attributes = 0; if (NativeMethods.SHILCreateFromPath(SelectedPath, out idl, ref attributes) == 0) { if (NativeMethods.SHCreateShellItem(IntPtr.Zero, IntPtr.Zero, idl, out item) == 0) { dialog.SetFolder(item); } if (idl != IntPtr.Zero) { Marshal.FreeCoTaskMem(idl); } } } if (!string.IsNullOrEmpty(Title)) { dialog.SetTitle(Title); } var hr = dialog.Show(owner); // 選択のキャンセルまたは例外 if (hr == ERROR_CANCELLED) { return(DialogResult.Cancel); } if (hr != 0) { return(DialogResult.Abort); } dialog.GetResult(out item); if (item != null) { item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out selectedPath); SelectedPath = selectedPath; } else { return(DialogResult.Abort); } return(DialogResult.OK); } finally { Marshal.FinalReleaseComObject(dialog); } }