Esempio n. 1
0
        public string BrowseDestination(Window owner, string destination, string extension)
        {
            using (var dialog = new FileSaveDialog())
            {
                GetFilePathInfo(destination, out var folderPath, out var fileName, out var fileExtension);

                dialog.SetClientGuid(new Guid("486640B6-B311-4EFD-A15F-616F51FAAF5B"));
                dialog.SetDefaultFolder(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
                dialog.SetTitle("Destination file of the image to be created");
                dialog.SetOkButtonLabel("Select");
                dialog.SetCancelButtonLabel("Cancel");
                dialog.SetFileNameLabel("Destination file :");
                dialog.SetDefaultExtension(extension.Substring(1));
                dialog.SetFileTypes($"Image files (*{extension})|*{extension}|All files (*.*)|*.*");
                dialog.SetFileTypeIndex(fileExtension == null || fileExtension.Equals(extension, StringComparison.OrdinalIgnoreCase) ? 1 : 2);
                dialog.DontAddToRecent = true;

                if (fileName != null)
                {
                    dialog.SetFileName(fileName);
                }

                if (PathHelper.DirectoryExists(folderPath))
                {
                    dialog.SetFolder(folderPath);
                }

                return(dialog.ShowDialog(owner) == true?dialog.GetResult() : null);
            }
        }
Esempio n. 2
0
        int NativeInterfaces.IFileDialogEvents.OnFileOk(NativeInterfaces.IFileDialog pfd)
        {
            int hr = NativeConstants.S_OK;

            NativeInterfaces.IShellItem shellItem = null;

            if (NativeMethods.SUCCEEDED(hr))
            {
                hr = FileSaveDialog.GetResult(out shellItem);
            }

            if (!NativeMethods.SUCCEEDED(hr))
            {
                throw Marshal.GetExceptionForHR(hr);
            }

            string pathName = null;

            try
            {
                shellItem.GetDisplayName(NativeConstants.SIGDN.SIGDN_FILESYSPATH, out pathName);
            }

            finally
            {
                if (shellItem != null)
                {
                    try
                    {
                        Marshal.ReleaseComObject(shellItem);
                    }

                    catch (Exception)
                    {
                    }

                    shellItem = null;
                }
            }

            string pathNameResolved = ResolveName(pathName);

            NativeInterfaces.IOleWindow oleWindow = (NativeInterfaces.IOleWindow)pfd;

            try
            {
                IntPtr hWnd = IntPtr.Zero;
                oleWindow.GetWindow(out hWnd);
                Win32Window win32Window = new Win32Window(hWnd, oleWindow);

                // File name/path validation
                if (hr >= 0)
                {
                    try
                    {
                        // Verify that these can be parsed correctly
                        string fileName = Path.GetFileName(pathNameResolved);
                        string dirName  = Path.GetDirectoryName(pathNameResolved);
                    }

                    catch (Exception ex)
                    {
                        if (!FileDialogUICallbacks.ShowError(win32Window, pathNameResolved, ex))
                        {
                            throw;
                        }

                        hr = NativeConstants.S_FALSE;
                    }
                }

                if (hr >= 0)
                {
                    // Overwrite existing file
                    if (!OverwritePrompt)
                    {
                        hr = NativeConstants.S_OK;
                    }
                    else if (File.Exists(pathNameResolved))
                    {
                        FileOverwriteAction action = FileDialogUICallbacks.ShowOverwritePrompt(win32Window, pathNameResolved);

                        switch (action)
                        {
                        case FileOverwriteAction.Cancel:
                            hr = NativeConstants.S_FALSE;
                            break;

                        case FileOverwriteAction.Overwrite:
                            hr = NativeConstants.S_OK;
                            break;

                        default:
                            throw new InvalidEnumArgumentException();
                        }
                    }
                }
            }

            catch (Exception)
            {
            }

            finally
            {
                try
                {
                    Marshal.ReleaseComObject(oleWindow);
                }

                catch (Exception)
                {
                }

                oleWindow = null;
            }

            return(hr);
        }