private void addIPPort() { 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(); var remotePort = 9100; try { if (IP != null && IP.Contains(":")) { var arIP = IP.Split(':'); if (arIP.Length == 2) remotePort = int.Parse(arIP[1]); } } catch (Exception ex) { Log.Error(LogName, "Could not parse port from IP"); Log.Error(LogName, ex); } mPort.SetPropertyValue("Name", Port); mPort.SetPropertyValue("Protocol", 1); mPort.SetPropertyValue("HostAddress", IP); mPort.SetPropertyValue("PortNumber", remotePort); mPort.SetPropertyValue("SNMPEnabled", false); var put = new PutOptions { UseAmendedQualifiers = true, Type = PutType.UpdateOrCreate }; mPort.Put(put); }
public Boolean install(long maxWait) { long waited = 0L; int port = 9100; if (isComplete()) { if (maxWait > 0) { //if ( strFile.Length == 0 || File.Exists(strFile)) //{ if (strIP != null && strIP.Length > 0 ) { if (strIP.Contains(":")) { String[] arIP = strIP.Split(new char[] { ':' }); if (arIP.Length == 2) { strIP = arIP[0]; try { port = Int32.Parse( arIP[1] ); } catch { } } } ConnectionOptions conn = new ConnectionOptions(); conn.EnablePrivileges = true; conn.Impersonation = ImpersonationLevel.Impersonate; ManagementPath mPath = new ManagementPath("Win32_TCPIPPrinterPort"); ManagementScope mScope = new ManagementScope(@"\\.\root\cimv2", conn); mScope.Options.EnablePrivileges = true; mScope.Options.Impersonation = ImpersonationLevel.Impersonate; ManagementObject mPort = new ManagementClass(mScope, mPath, null).CreateInstance(); mPort.SetPropertyValue("Name", "IP_" + strIP); mPort.SetPropertyValue("Protocol", 1); mPort.SetPropertyValue("HostAddress", strIP); mPort.SetPropertyValue("PortNumber", port); mPort.SetPropertyValue("SNMPEnabled", false); PutOptions put = new PutOptions(); put.UseAmendedQualifiers = true; put.Type = PutType.UpdateOrCreate; mPort.Put(put); } Process proc; Boolean blIsIPP = false; if (strPort.Trim().ToLower().StartsWith("ipp://")) { // iprint installation proc = new Process(); proc.StartInfo.FileName = @"c:\windows\system32\iprntcmd.exe"; proc.StartInfo.Arguments = " -a no-gui \"" + strPort + "\""; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; proc.Start(); blIsIPP = true; } else if (strAlias.Trim().ToLower().StartsWith("\\\\")) { // Add per machine printer connection proc = Process.Start("rundll32.exe", " printui.dll,PrintUIEntry /ga /n " + strAlias); proc.WaitForExit(120000); // Add printer network connection, download the drivers from the print server proc = Process.Start("rundll32.exe", " printui.dll,PrintUIEntry /in /n " + strAlias); proc.WaitForExit(120000); bounceSpooler(); } else { // Normal Installation proc = Process.Start("rundll32.exe", getInstallArguments()); } while (!proc.HasExited) { sleep(100); waited += 100; if (waited > maxWait) { proc.Kill(); strError = "Max install time exceeded (" + maxWait + ")"; return false; } } if (blIsIPP) { strError = "IPP Return codes unknown"; return true; } if (proc.ExitCode == 0) { strError = ""; return true; } //} //else //strError = "Printer Definition file was not found!"; //strError = "Strfile:" + strFile + "."; } else strError = "Max wait time was less than zero!"; } else strError = "Printer information is incomplete"; return false; }
public override void Add() { Log.Entry(LogName, "Attempting to add printer:"); Log.Entry(LogName, string.Format("--> Name = {0}", Name)); Log.Entry(LogName, string.Format("--> IP = {0}", IP)); Log.Entry(LogName, string.Format("--> Port = {0}", Port)); if (string.IsNullOrEmpty(IP) || !Name.StartsWith("\\\\")) return; if (IP.Contains(":")) { var arIP = IP.Split(':'); if (arIP.Length == 2) { IP = arIP[0]; Port = arIP[1]; } } 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(); if (mPort != null) { mPort.SetPropertyValue("Name", "IP_" + IP); mPort.SetPropertyValue("Protocol", 1); mPort.SetPropertyValue("HostAddress", IP); mPort.SetPropertyValue("PortNumber", Port); mPort.SetPropertyValue("SNMPEnabled", false); var put = new PutOptions { UseAmendedQualifiers = true, Type = PutType.UpdateOrCreate }; mPort.Put(put); } if (!Name.StartsWith("\\\\")) return; // Add per machine printer connection var proc = Process.Start("rundll32.exe", " printui.dll,PrintUIEntry /ga /n " + Name); if (proc != null) proc.WaitForExit(120000); // Add printer network connection, download the drivers from the print server proc = Process.Start("rundll32.exe", " printui.dll,PrintUIEntry /in /n " + Name); if (proc != null) proc.WaitForExit(120000); }