public void Add(Printer printer, bool verbose = false) { try { switch (printer.Type) { case Printer.PrinterType.iPrint: AddiPrint(printer, verbose); break; case Printer.PrinterType.Local: AddLocal(printer,verbose); break; case Printer.PrinterType.Network: AddNetwork(printer, verbose); break; case Printer.PrinterType.CUPS: AddCUPS(printer, verbose); break; } } catch (Exception ex) { Log.Error(LogName, "Could not add"); Log.Error(LogName, ex); } }
public void Add(Printer printer) { Log.Entry(LogName, "Attempting to add printer:"); if (printer.Name != null) Log.Entry(LogName, $"--> Name = {printer.Name}"); if (printer.IP != null) Log.Entry(LogName, $"--> IP = {printer.IP}"); if (printer.Port != null) Log.Entry(LogName, $"--> Port = {printer.Port}"); if (printer.File != null) Log.Entry(LogName, $"--> File = {printer.File}"); if (printer.Model != null) Log.Entry(LogName, $"--> Model = {printer.Model}"); try { if (printer is iPrintPrinter) AddiPrint((iPrintPrinter) printer); else if (printer is LocalPrinter) AddLocal((LocalPrinter) printer); else if (printer is NetworkPrinter) AddNetwork((NetworkPrinter) printer); } catch (Exception ex) { Log.Error(LogName, "Could not add"); Log.Error(LogName, ex); } }
protected override void AddCUPS(Printer printer, bool verbose = false) { string[] stdout; ProcessHandler.Run("echo", $"{printer.Name} | tr ' ' '_'", true, out stdout); var portName = stdout[0]; var lpdAddress = $"lpd://{printer.IP}"; ProcessHandler.Run("lpadmin", $"-p {portName} -E -v {lpdAddress} -P {printer.File} -D {printer.Name}"); }
public override void Configure(Printer printer, bool verbose = false) { if (printer.Type == Printer.PrinterType.Network) { Log.Entry(LogName, $"Invoking add {printer.Name} for all users"); PrintUI($"/in /n \"{printer.Name}\"", verbose); } if (string.IsNullOrEmpty(printer.ConfigFile)) return; Log.Entry(LogName, "Configuring " + printer.Name); PrintUI($"/Sr /n \"{printer.Name}\" /a \"{printer.ConfigFile}\" m f g p", verbose); }
private void addButton_Click(object sender, EventArgs e) { aliasBox.Enabled = false; ipBox.Enabled = false; portBox.Enabled = false; modelBox.Enabled = false; driverBox.Enabled = false; typeCombo.Enabled = false; // Create the printer var printer = new Printer { Name = aliasBox.Text, File = driverBox.Text, IP = ipBox.Text, Port = portBox.Text, Model = modelBox.Text }; if (typeCombo.SelectedItem.Equals("TCP/IP")) { printer.Type = Printer.PrinterType.Local; } else if (typeCombo.SelectedItem.Equals("Network")) { printer.Type = Printer.PrinterType.Network; } else if (typeCombo.SelectedItem.Equals("iPrint")) { printer.Type = Printer.PrinterType.iPrint; } printer.Add(_instance, true); aliasBox.Enabled = true; ipBox.Enabled = true; portBox.Enabled = true; modelBox.Enabled = true; driverBox.Enabled = true; typeCombo.Enabled = true; }
protected override void AddCUPS(Printer printer, bool verbose = false) { throw new NotImplementedException(); }
protected override void AddNetwork(Printer printer, bool verbose = false) { PrintUI($"/ga /n \"{printer.Name}\"", verbose); }
public override void Configure(Printer printer, bool verbose = false) { throw new NotImplementedException(); }
protected override void AddLocal(Printer printer, bool verbose = false) { if (printer.IP != null) AddIPPort(printer, "9100"); PrintUI($"/if /b \"{printer.Name}\" /f \"{printer.File}\" /r \"{printer.Port}\" /m \"{printer.Model}\"", verbose); }
public abstract void Configure(Printer printer, bool verbose = false);
protected abstract void AddCUPS(Printer printer, bool verbose = false);
protected abstract void AddNetwork(Printer printer, bool verbose = false);
protected abstract void AddLocal(Printer printer, bool verbose = false);
protected override void AddiPrint(Printer printer, bool verbose = false) { var proc = new Process { StartInfo = { FileName = @"c:\windows\system32\iprntcmd.exe", Arguments = " -a no-gui \"" + printer.Port + "\"", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden } }; proc.Start(); proc.WaitForExit(120000); Log.Entry(LogName, "Return code " + proc.ExitCode); }
private void AddIPPort(Printer printer, string remotePort) { var conn = new ConnectionOptions { EnablePrivileges = true, Impersonation = ImpersonationLevel.Impersonate }; var mPath = new ManagementPath("Win32_TCPIPPrinterPort"); var mScope = new ManagementScope(@"\\.\root\cimv2", conn) { Options = { EnablePrivileges = true, Impersonation = ImpersonationLevel.Impersonate } }; var mPort = new ManagementClass(mScope, mPath, null).CreateInstance(); try { if (printer.IP != null && printer.IP.Contains(":")) { var arIP = printer.IP.Split(':'); if (arIP.Length == 2) remotePort = int.Parse(arIP[1]).ToString(); } } catch (Exception ex) { Log.Error(LogName, "Could not parse port from IP"); Log.Error(LogName, ex); } mPort.SetPropertyValue("Name", printer.Port); mPort.SetPropertyValue("Protocol", 1); mPort.SetPropertyValue("HostAddress", printer.IP); mPort.SetPropertyValue("PortNumber", remotePort); mPort.SetPropertyValue("SNMPEnabled", false); var put = new PutOptions { UseAmendedQualifiers = true, Type = PutType.UpdateOrCreate }; mPort.Put(put); }