Printing to a network printer in C #

I am trying to print a network servitude through C # in VS2010, but have encountered difficulties when working with it. If I use "print", Verb insted, it prints accurately, but only for the default printer. I use PrintTo Verb to try to specify a printer. In my case, using the print verb, I can successfully print on the same network printer that I am trying to print using the printto verb after I changed my default printer to another printer. Here is the code I'm using now. Any help would be greatly appreciated.

private string FindPrinter(string printerName) { string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName); ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); ManagementObjectCollection printers = searcher.Get(); foreach (ManagementObject printer in printers) { if (!String.IsNullOrEmpty(printer.Properties["PortName"].Value.ToString())) { return printerName = string.Format(@"\\{0}\{1}", printer.Properties["PortName"].Value.ToString(), printerName); } } return printerName; } private void Print(string fileName, string printerName) { PrinterSettings ps = new PrinterSettings(); ps.PrinterName = printerName; if (ps.IsValid) { try { ProcessStartInfo processStartInfo = new ProcessStartInfo(fileName); using (PrintDialog pd = new PrintDialog()) { pd.ShowDialog(); printerName = this.FindPrinter(pd.PrinterSettings.PrinterName); if (printerName.IndexOf(@"\\") == 0) { processStartInfo.Verb = "PrintTo"; processStartInfo.Arguments = printerName; } else { processStartInfo.Verb = "print"; } } processStartInfo.CreateNoWindow = true; processStartInfo.WindowStyle = ProcessWindowStyle.Hidden; Process printProcess = new Process(); printProcess.StartInfo = processStartInfo; bool printStarted = printProcess.Start(); MessageBox.Show(string.Format("{0} printed to {1}", fileName, printerName), "Report Print", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Report Print", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show(string.Format("{0} printer does not exist. Please contact technical support.", printerName), "Report Print", MessageBoxButtons.OK, MessageBoxIcon.Error); } } 
+4
source share
1 answer

use only the verb PrintTo and use double quotes to indicate the name of the printer

 processStartInfo.Verb = "PrintTo"; processStartInfo.Arguments = "\"" + printerName + "\""; 
+2
source

All Articles