public IEnumerable <string> PrinterNames() { PrinterSystem PrnSystem = mcsApp.PrinterSystem(); Strings names = PrnSystem.Printers(enumKindOfPrinters.lppxAllPrinters); for (int i = 1; i <= names.Count; i++) { yield return(names.Item(i)); } Marshal.ReleaseComObject(names); Marshal.ReleaseComObject(PrnSystem); }
public bool SwitchPrinter(Document doc, string printerName) { PrinterSystem PrnSystem = mcsApp.PrinterSystem(); Strings names = PrnSystem.Printers(enumKindOfPrinters.lppxAllPrinters); string currentName = doc.Printer.Name; string fullName = doc.Printer.FullName; string eachName = ""; if (currentName != printerName && fullName != printerName) { short count = names.Count; for (short i = 1; i <= count; i++) { fullName = names.Item(i); int pos = fullName.LastIndexOf(','); if (pos != -1) { eachName = fullName.Substring(0, pos); if (eachName == printerName || fullName == printerName) { bool bDirectAccess = false; string PortName = fullName.Substring(pos + 1); if (PortName.StartsWith("->")) { PortName = PortName.Substring(2); bDirectAccess = true; } var printer = doc.Printer; printer.SwitchTo(printerName, PortName, bDirectAccess); Marshal.ReleaseComObject(printer); return(true); } } } Marshal.ReleaseComObject(names); Marshal.ReleaseComObject(PrnSystem); return(false); } return(true); }