public class Class1 { public void printxps() { Thread printingThread = new Thread(BatchXPSPrinter.PrintXPS); // Set the thread that will use PrintQueue.AddJob to single threading. printingThread.SetApartmentState(ApartmentState.STA); // Start the printing thread. The method passed to the Thread // constructor will execute. printingThread.Start(); } } public class BatchXPSPrinter { public static void PrintXPS() { // Create print server and print queue. LocalPrintServer localPrintServer = new LocalPrintServer(); PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue(); // Prompt user to identify the directory, and then create the directory object. String directoryPath = "D:\\"; DirectoryInfo dir = new DirectoryInfo(directoryPath); // If the user mistyped, end the thread and return to the Main thread. if (!dir.Exists) { throw new Exception("Invalid Drive"); } else { // If there are no XPS files in the directory, end the thread // and return to the Main thread. if (dir.GetFiles("*.xps").Length == 0) { throw new Exception("Invalid File"); } else { // Batch process all XPS files in the directory. foreach (FileInfo f in dir.GetFiles("*.xps")) { String nextFile = directoryPath + "\\" + f.Name; // Console.WriteLine("Adding {0} to queue.", nextFile); try { // Print the Xps file while providing XPS validation and progress notifications. PrintSystemJobInfo xpsPrintJob = defaultPrintQueue.AddJob(f.Name, nextFile,false); } catch (Exception e) { } }// end for each XPS file }//end if there are no XPS files in the directory }//end if the directory does not exist }// end PrintXPS method }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)