public bool?ShowDialog() { var openDialogCoClass = new INativeFileOpenDialog(); // Template method to allow derived dialog to create actual specific COM coclass (e.g. FileOpenDialog or FileSaveDialog) try { ApplyNativeSettings(openDialogCoClass); // Apply outer properties to native dialog instance var parentWindow = Helpers.GetDefaultOwnerWindow(); int hresult = openDialogCoClass.Show(GetHandleFromWindow(parentWindow)); if (ErrorHelper.Matches(hresult, Win32ErrorCode.ERROR_CANCELLED)) // Create return information { return(false); } openDialogCoClass.GetResults(out var resultsArray); resultsArray.GetCount(out var count); if (count > 0) { resultsArray.GetItemAt(0, out var shellItem); shellItem.GetDisplayName(NativeMethods.SIGDN.DESKTOPABSOLUTEPARSING, out var file); FileName = file; } return(true); } finally { Marshal.ReleaseComObject(openDialogCoClass); } }
public Nullable <bool> ShowDialog(Window window) { INativeFileOpenDialog openDialogCoClass = new INativeFileOpenDialog(); try { ApplyNativeSettings(openDialogCoClass); window ??= Helpers.GetDefaultOwnerWindow(); int hresult = openDialogCoClass.Show(GetHandleFromWindow(window)); if (ErrorHelper.Matches(hresult, Win32ErrorCode.ERROR_CANCELLED)) { return(false); } openDialogCoClass.GetResults(out IShellItemArray resultsArray); resultsArray.GetCount(out uint count); if (count <= 0) { return(true); } resultsArray.GetItemAt(0, out IShellItem shellItem); shellItem.GetDisplayName(NativeMethods.SIGDN.DESKTOPABSOLUTEPARSING, out string file); FileName = file; FileNames = new string[count]; for (uint i = 0; i < count; i++) { resultsArray.GetItemAt(i, out shellItem); shellItem.GetDisplayName(NativeMethods.SIGDN.DESKTOPABSOLUTEPARSING, out file); FileNames[i] = file; } return(true); } finally { Marshal.ReleaseComObject(openDialogCoClass); } }