static bool GetPdfOutputFilename(ref String outputFile) { bool filenameRetrieved = false; switch (Properties.Settings.Default.AskUserForOutputFilename) { case (true): using (SetOutputFilename dialogOwner = new SetOutputFilename()) { dialogOwner.TopMost = true; dialogOwner.TopLevel = true; dialogOwner.Show(); // Form won't actually show - Application.Run() never called // but having a topmost/toplevel owner lets us bring the SaveFileDialog to the front dialogOwner.BringToFront(); using (SaveFileDialog pdfFilenameDialog = new SaveFileDialog()) { pdfFilenameDialog.AddExtension = true; pdfFilenameDialog.AutoUpgradeEnabled = true; pdfFilenameDialog.CheckPathExists = true; pdfFilenameDialog.Filter = "pdf files (*.pdf)|*.pdf"; pdfFilenameDialog.ShowHelp = false; pdfFilenameDialog.Title = "PDF Scribe - Set output filename"; pdfFilenameDialog.ValidateNames = true; if (pdfFilenameDialog.ShowDialog(dialogOwner) == DialogResult.OK) { outputFile = pdfFilenameDialog.FileName; filenameRetrieved = true; } } dialogOwner.Close(); } break; default: outputFile = GetOutputFilename(); filenameRetrieved = true; break; } return(filenameRetrieved); }
static bool GetPdfOutputFilename(ref String outputFile) { bool filenameRetrieved = false; switch (Properties.Settings.Default.AskUserForOutputFilename) { case (true): using (SetOutputFilename dialogOwner = new SetOutputFilename()) { dialogOwner.TopMost = true; dialogOwner.TopLevel = true; dialogOwner.Show(); // Form won't actually show - Application.Run() never called // but having a topmost/toplevel owner lets us bring the SaveFileDialog to the front dialogOwner.BringToFront(); using (SaveFileDialog pdfFilenameDialog = new SaveFileDialog()) { pdfFilenameDialog.AddExtension = true; pdfFilenameDialog.AutoUpgradeEnabled = true; pdfFilenameDialog.CheckPathExists = true; pdfFilenameDialog.Filter = "pdf files (*.pdf)|*.pdf"; pdfFilenameDialog.ShowHelp = false; pdfFilenameDialog.Title = "PDF Scribe - Set output filename"; pdfFilenameDialog.ValidateNames = true; if (pdfFilenameDialog.ShowDialog(dialogOwner) == DialogResult.OK) { outputFile = pdfFilenameDialog.FileName; filenameRetrieved = true; } } dialogOwner.Close(); } break; default: try { outputFile = GetOutputFilename(); // Test if we can write to the destination using (FileStream newOutputFile = File.Create(outputFile)) { } File.Delete(outputFile); filenameRetrieved = true; } catch (Exception ex) when(ex is ArgumentException || ex is ArgumentNullException || ex is NotSupportedException || ex is DirectoryNotFoundException) { logEventSource.TraceEvent(TraceEventType.Error, (int)TraceEventType.Error, errorDialogOutputFilenameInvalid + Environment.NewLine + "Exception message: " + ex.Message); DisplayErrorMessage(errorDialogCaption, errorDialogOutputFilenameInvalid); } catch (PathTooLongException ex) { // filename is greater than 260 characters logEventSource.TraceEvent(TraceEventType.Error, (int)TraceEventType.Error, errorDialogOutputFilenameTooLong + Environment.NewLine + "Exception message: " + ex.Message); DisplayErrorMessage(errorDialogCaption, errorDialogOutputFilenameTooLong); } catch (UnauthorizedAccessException ex) { logEventSource.TraceEvent(TraceEventType.Error, (int)TraceEventType.Error, errorDialogOutputFileAccessDenied + Environment.NewLine + "Exception message: " + ex.Message); // Can't write to target dir DisplayErrorMessage(errorDialogCaption, errorDialogOutputFileAccessDenied); } break; } return(filenameRetrieved); }