private void SetDialogProperties(NativeMethods.IFileDialog dialog) { if (!String.IsNullOrEmpty(Description)) { if (UseDescriptionForTitle) { dialog.SetTitle(Description); } else { ((NativeMethods.IFileDialogCustomize)dialog).AddText(0, Description); } } dialog.SetOptions( NativeMethods.FOS.FOS_PICKFOLDERS | NativeMethods.FOS.FOS_FORCEFILESYSTEM | NativeMethods.FOS.FOS_FILEMUSTEXIST ); if (!String.IsNullOrEmpty(SelectedPath)) { string parent = Path.GetDirectoryName(SelectedPath); if (parent == null || !Directory.Exists(parent)) { dialog.SetFileName(SelectedPath); } else { string folder = Path.GetFileName(SelectedPath); dialog.SetFolder(CreateItemFromParsingName(parent)); dialog.SetFileName(folder); } } }
public DialogResult ShowVistaDialog(IWin32Window owner) { NativeMethods.IFileDialog frm = (NativeMethods.IFileDialog) new NativeMethods.FileOpenDialogRCW(); frm.GetOptions(out uint options); options |= NativeMethods.FOS_PICKFOLDERS | NativeMethods.FOS_FORCEFILESYSTEM | NativeMethods.FOS_NOVALIDATE | NativeMethods.FOS_NOTESTFILECREATE | NativeMethods.FOS_DONTADDTORECENT; frm.SetOptions(options); if (InitialFolder != null) { Guid riid = new Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"); // IShellItem if (NativeMethods.SHCreateItemFromParsingName( InitialFolder, IntPtr.Zero, ref riid, out NativeMethods.IShellItem directoryShellItem) == NativeMethods.S_OK) { frm.SetFolder(directoryShellItem); } } if (DefaultFolder != null) { Guid riid = new Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"); // IShellItem if (NativeMethods.SHCreateItemFromParsingName( DefaultFolder, IntPtr.Zero, ref riid, out NativeMethods.IShellItem directoryShellItem) == NativeMethods.S_OK) { frm.SetDefaultFolder(directoryShellItem); } } if (owner != null && frm.Show(owner.Handle) == NativeMethods.S_OK) { if (frm.GetResult(out NativeMethods.IShellItem shellItem) == NativeMethods.S_OK) { if (shellItem.GetDisplayName( NativeMethods.SIGDN_FILESYSPATH, out IntPtr pszString) == NativeMethods.S_OK) { if (pszString != IntPtr.Zero) { try { Folder = Marshal.PtrToStringAuto(pszString); return(DialogResult.OK); } finally { Marshal.FreeCoTaskMem(pszString); } } } } } return(DialogResult.Cancel); }