I have an ASP.NET app on a domain server, linked to network printers over IP ports.
When I Use PrintToPrinter() function with the good printer name found, it sometimes not goes through the good printer.
Code:
public void Print() { if (((TraceSwitch)Tracing.Switches["development"]).Level >= TraceLevel.Verbose) { string printerName = "Printer Laser 1" string installedPrinters = "Installed Printers: " + String.Join(",", PrinterSettings.InstalledPrinters.Cast<string>().ToList()); Tracing.Write(installedPrinters, TraceLevel.Verbose); Tracing.Write("Report printer name: " + printerName, TraceLevel.Verbose); } bool printerExists = false; // Not using linq here, i want see where the compare does not work at the client site. foreach (string printer in PrinterSettings.InstalledPrinters) { Tracing.Write("Compare Installed printer: " + printer + ", Report printer: " + printerName, TraceLevel.Verbose); if (printer.Equals(printerName, StringComparison.InvariantCultureIgnoreCase)) { Tracing.Write("Compare successful", TraceLevel.Verbose); printerExists = true; break; } } if (printerExists) { Tracing.Write("Printer found: " + printerName, TraceLevel.Verbose); Print(printerName); } else { Print(null); } } public void Print(string printerName) { CrystalReportSource crs = null; ReportDocument reportDoc = null; PrintDocument docToPrint = null; try { crs = GetCrystalReportSource(this.DataSet(), FullReportName); reportDoc = crs.ReportDocument; //A print Document is needed to be able to loop through the PaperSize based on a selected printer docToPrint = new PrintDocument(); //If the printer exists, it will set it as the printer then the PaperSize collection will change and we can get the right one. if (printerName != null) { docToPrint.PrinterSettings.PrinterName = printerName; reportDoc.PrintOptions.NoPrinter = false; reportDoc.PrintOptions.PrinterName = printerName; PaperSize sizes = null; foreach (PaperSize size in docToPrint.PrinterSettings.PaperSizes) { if (size.PaperName == this.AppReport.PaperName) sizes = size; } if (sizes != null) { docToPrint.PrinterSettings.DefaultPageSettings.PaperSize = sizes; reportDoc.PrintOptions.PaperSize = (CrystalDecisions.Shared.PaperSize)sizes.RawKind; } } reportDoc.PrintToPrinter(1, false, 0, 0); } finally { // Release disposable resource if (docToPrint != null) { docToPrint.Dispose(); docToPrint = null; } if (crs != null) { DisposeCrystalReportSource(crs); //dispose every sub section of the report crs = null; GC.Collect(); // Force garbage collection to alleviate infamous Crystal Reports leaks. } } }