private void SetNativeDialogProperties(NativeFileOpenDialog nativeFileOpenDialog) { //Set Title if (!string.IsNullOrEmpty(m_Title)) { nativeFileOpenDialog.SetTitle(m_Title); } //Set Folder if (!string.IsNullOrEmpty(m_FolderPath)) { if (System.IO.Directory.Exists(m_FolderPath)) { nativeFileOpenDialog.SetFolder(CreateShellItemFromParsingName(m_FolderPath)); } else { string parent = System.IO.Path.GetDirectoryName(m_FolderPath); if (parent != null && System.IO.Directory.Exists(parent)) { string folder = System.IO.Path.GetFileName(m_FolderPath); nativeFileOpenDialog.SetFolder(CreateShellItemFromParsingName(parent)); nativeFileOpenDialog.SetFileName(folder); } } } //Apply option bitflags nativeFileOpenDialog.SetOptions(GetNativeDialogFlags()); }
public DialogResult ShowDialog() { NativeFileOpenDialog dialog = null; try { // If the caller did not specify a starting path, or set it to null, // it is not healthy as it causes SHCreateItemFromParsingName to // throw E_INVALIDARG (0x80070057). Setting it to an empty string. // if (SelectedPath == null) { SelectedPath = string.Empty; } dialog = new NativeFileOpenDialog(); dialog.SetTitle(Title); object shellItem; // IShellItem GUID Guid guid = new Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"); int hresult = SHCreateItemFromParsingName(SelectedPath, IntPtr.Zero, ref guid, out shellItem); if ((uint)hresult != (uint)HRESULT.S_OK) { throw Marshal.GetExceptionForHR(hresult); } dialog.SetFolder((IShellItem)shellItem); dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM | FOS.FOS_FILEMUSTEXIST); IntPtr hWnd = new WindowInteropHelper(Owner).Handle; hresult = dialog.Show(hWnd); if (hresult < 0) { if ((uint)hresult == (uint)HRESULT.E_CANCELLED) { return(DialogResult.Cancel); } throw Marshal.GetExceptionForHR(hresult); } string path; IShellItem item; dialog.GetResult(out item); item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out path); SelectedPath = path; return(DialogResult.OK); } finally { if (dialog != null) { Marshal.FinalReleaseComObject(dialog); } } }
public DialogResult ShowDialog() { NativeFileOpenDialog dialog = null; try { // If the caller did not specify a starting path, or set it to null, // it is not healthy as it causes SHCreateItemFromParsingName to // throw E_INVALIDARG (0x80070057). Setting it to an empty string. // if (SelectedPath == null) SelectedPath = string.Empty; dialog = new NativeFileOpenDialog(); dialog.SetTitle(Title); object shellItem; // IShellItem GUID Guid guid = new Guid("43826D1E-E718-42EE-BC55-A1E261C37BFE"); int hresult = SHCreateItemFromParsingName(SelectedPath, IntPtr.Zero, ref guid, out shellItem); if ((uint)hresult != (uint)HRESULT.S_OK) throw Marshal.GetExceptionForHR(hresult); dialog.SetFolder((IShellItem)shellItem); dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM | FOS.FOS_FILEMUSTEXIST); IntPtr hWnd = new WindowInteropHelper(Owner).Handle; hresult = dialog.Show(hWnd); if (hresult < 0) { if ((uint)hresult == (uint)HRESULT.E_CANCELLED) return DialogResult.Cancel; throw Marshal.GetExceptionForHR(hresult); } string path; IShellItem item; dialog.GetResult(out item); item.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out path); SelectedPath = path; return DialogResult.OK; } finally { if (dialog != null) Marshal.FinalReleaseComObject(dialog); } }