/// <summary> /// The main method call to print an array of files in a specified folder /// </summary> /// <param name="ACaller">The calling screen. This must implement the IPrintProgress interface</param> /// <param name="APrinterName">Name of the printer to print to</param> /// <param name="AFolderPath">Full path to the folder containing the files</param> /// <param name="AFiles">The files to print</param> public void StartPrintingAsync(IPrintProgress ACaller, string APrinterName, string AFolderPath, string[] AFiles) { FCallerForm = ACaller; BackgroundWorkerArgument args = new BackgroundWorkerArgument(APrinterName, AFolderPath, AFiles); FBackgroundWorker.RunWorkerAsync(args); }
/// <summary> /// The main method call to print all files in a specified folder. The method displays a Printer Dialog /// </summary> /// <param name="ACaller">The calling screen. This must implement the IPrintProgress interface</param> /// <param name="AFolderPath">Full path to the folder containing the files</param> public void StartPrintingAsync(IPrintProgress ACaller, string AFolderPath) { FCallerForm = ACaller; string printerName = null; using (PrintDialog pd = new PrintDialog()) { if (pd.ShowDialog() != DialogResult.OK) { RunWorkerCompletedEventArgs completedArgs = new RunWorkerCompletedEventArgs(null, null, true); ACaller.AsyncPrintingCompleted(completedArgs); return; } // Remember this for use lower down printerName = String.Format("\"{0}\"", pd.PrinterSettings.PrinterName); } // Get all the files in the specified folder string[] allFiles = Directory.GetFiles(AFolderPath); // Create a sorted list of the files (they will be named in sequence) SortedList <string, string> sortedFiles = new SortedList <string, string>(); foreach (string fullPath in allFiles) { string fileName = Path.GetFileName(fullPath); sortedFiles.Add(fileName.Substring(0, 4), fileName); } // Now create a simple list from the sorted list List <string> fileList = new List <string>(); foreach (KeyValuePair <string, string> kvp in sortedFiles) { fileList.Add(kvp.Value); } // And call our async method BackgroundWorkerArgument args = new BackgroundWorkerArgument(printerName, AFolderPath, fileList.ToArray()); FBackgroundWorker.RunWorkerAsync(args); }