private void SetDialogProperties(IFileOpenDialog dialog) { dialog.SetOptions(FOS.FOS_PICKFOLDERS | FOS.FOS_FORCEFILESYSTEM); 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); } } }
private static String[] ShowDialogInner(IFileOpenDialog dialog, IntPtr parentWindowHandle, String title, String initialDirectory, String defaultFileName, IReadOnlyCollection <Filter> filters, Int32 selectedFilterZeroBasedIndex, FileOpenOptions flags) { flags = flags | FileOpenOptions.NoTestFileCreate | FileOpenOptions.PathMustExist | FileOpenOptions.ForceFilesystem; dialog.SetOptions(flags); if (title != null) { dialog.SetTitle(title); } if (initialDirectory != null) { IShellItem2 initialDirectoryShellItem = Utility.ParseShellItem2Name(initialDirectory); if (initialDirectoryShellItem != null) { dialog.SetFolder(initialDirectoryShellItem); } } if (defaultFileName != null) { dialog.SetFileName(defaultFileName); } Utility.SetFilters(dialog, filters, selectedFilterZeroBasedIndex); HResult result = dialog.Show(parentWindowHandle); if (result == HResult.Ok) { IShellItemArray resultsArray; dialog.GetResults(out resultsArray); String[] fileNames = Utility.GetFileNames(resultsArray); return(fileNames); } else if (result == HResult_Win32_Canceled) { // Cancelled by user. return(null); } else { UInt32 win32ErrorCode = Utility.Win32ErrorFromHResult((UInt32)result); throw new Win32Exception(error: (Int32)win32ErrorCode); } }
private static IReadOnlyList <String> ShowDialogInner(IFileOpenDialog dialog, IntPtr parentHWnd, String title, String initialDirectory, String defaultFileName, IReadOnlyCollection <Filter> filters, Int32 selectedFilterZeroBasedIndex, FileOpenOptions flags) #endif { flags = flags | FileOpenOptions.NoTestFileCreate | FileOpenOptions.PathMustExist | FileOpenOptions.ForceFilesystem; dialog.SetOptions(flags); if (title != null) { dialog.SetTitle(title); } if (initialDirectory != null) { #if NETCOREAPP3_1_OR_GREATER IShellItem2?initialDirectoryShellItem = Utility.ParseShellItem2Name(initialDirectory); #else IShellItem2 initialDirectoryShellItem = Utility.ParseShellItem2Name(initialDirectory); #endif if (initialDirectoryShellItem != null) { dialog.SetFolder(initialDirectoryShellItem); } } if (defaultFileName != null) { dialog.SetFileName(defaultFileName); } Utility.SetFilters(dialog, filters, selectedFilterZeroBasedIndex); // HResult hr = dialog.Show(parentHWnd); if (hr.ValidateDialogShowHResult()) { dialog.GetResults(out IShellItemArray resultsArray); IReadOnlyList <String> fileNames = Utility.GetFileNames(resultsArray); return(fileNames); } else { // User cancelled. return(null); } }
private static String[] ShowDialogInner(IFileOpenDialog dialog, IntPtr parentHWnd, String title, String initialDirectory, String defaultFileName, IReadOnlyCollection <Filter> filters, Int32 selectedFilterZeroBasedIndex, FileOpenOptions flags) { flags = flags | FileOpenOptions.NoTestFileCreate | FileOpenOptions.PathMustExist | FileOpenOptions.ForceFilesystem; dialog.SetOptions(flags); if (title != null) { dialog.SetTitle(title); } if (initialDirectory != null) { IShellItem2 initialDirectoryShellItem = Utility.ParseShellItem2Name(initialDirectory); if (initialDirectoryShellItem != null) { dialog.SetFolder(initialDirectoryShellItem); } } if (defaultFileName != null) { dialog.SetFileName(defaultFileName); } Utility.SetFilters(dialog, filters, selectedFilterZeroBasedIndex); HResult result = dialog.Show(parentHWnd); HResult cancelledAsHResult = Utility.HResultFromWin32((int)HResult.Win32ErrorCanceled); if (result == cancelledAsHResult) { // Cancelled return(null); } else { // OK IShellItemArray resultsArray; dialog.GetResults(out resultsArray); String[] fileNames = Utility.GetFileNames(resultsArray); return(fileNames); } }