mercredi 16 avril 2014

réseautage - C# meilleure façon d'installer un pilote d'imprimante par programmation ? -Débordement de pile


I am adding network printers to a local machine and I am at a lost on how to load the printers driver onto the machine. I am using a piece of code I found online that makes adding the printer nice and easy, but with the driver you just select drive you need by name. I tried a few ways with printui.dll with command prompt, but each attempt failed. I have .INF files for each printer.


String portNumber = "9100";
String printerIP = "157.198.192.42";
String portName = "IP_" + printerIP;
ConnectionOptions options = new ConnectionOptions();
options.EnablePrivileges = true;
ManagementScope mscope = new ManagementScope(ManagementPath.DefaultPath, options);
mscope.Connect();

ManagementPath mpPort = new ManagementPath("Win32_TCPIPPrinterPort");
ManagementClass mcPort = new ManagementClass(mscope, mpPort, new ObjectGetOptions());
ManagementObject moPort = mcPort.CreateInstance();
moPort.Properties["Name"].Value = portName;
moPort.Properties["HostAddress"].Value = printerIP;
moPort.Properties["PortNumber"].Value = portNumber;
moPort.Properties["Protocol"].Value = 1;
moPort.Put();

ManagementPath mpPrinter = new System.Management.ManagementPath("Win32_Printer");
ManagementClass mcPrinter = new ManagementClass(mscope, mpPrinter, new ObjectGetOptions());
ManagementObject moPrinter = mcPrinter.CreateInstance();
moPrinter.Properties["Name"].Value = "Tool Engineering";
moPrinter.Properties["DeviceID"].Value = "Tool Engineering";
moPrinter.Properties["DriverName"].Value = "Canon iR C2880/C3380";
moPrinter.Properties["PortName"].Value = portName;
moPrinter.Properties["Network"].Value = true;
moPrinter.Properties["Shared"].Value = false;
moPrinter.Put();


I am adding network printers to a local machine and I am at a lost on how to load the printers driver onto the machine. I am using a piece of code I found online that makes adding the printer nice and easy, but with the driver you just select drive you need by name. I tried a few ways with printui.dll with command prompt, but each attempt failed. I have .INF files for each printer.


String portNumber = "9100";
String printerIP = "157.198.192.42";
String portName = "IP_" + printerIP;
ConnectionOptions options = new ConnectionOptions();
options.EnablePrivileges = true;
ManagementScope mscope = new ManagementScope(ManagementPath.DefaultPath, options);
mscope.Connect();

ManagementPath mpPort = new ManagementPath("Win32_TCPIPPrinterPort");
ManagementClass mcPort = new ManagementClass(mscope, mpPort, new ObjectGetOptions());
ManagementObject moPort = mcPort.CreateInstance();
moPort.Properties["Name"].Value = portName;
moPort.Properties["HostAddress"].Value = printerIP;
moPort.Properties["PortNumber"].Value = portNumber;
moPort.Properties["Protocol"].Value = 1;
moPort.Put();

ManagementPath mpPrinter = new System.Management.ManagementPath("Win32_Printer");
ManagementClass mcPrinter = new ManagementClass(mscope, mpPrinter, new ObjectGetOptions());
ManagementObject moPrinter = mcPrinter.CreateInstance();
moPrinter.Properties["Name"].Value = "Tool Engineering";
moPrinter.Properties["DeviceID"].Value = "Tool Engineering";
moPrinter.Properties["DriverName"].Value = "Canon iR C2880/C3380";
moPrinter.Properties["PortName"].Value = portName;
moPrinter.Properties["Network"].Value = true;
moPrinter.Properties["Shared"].Value = false;
moPrinter.Put();

0 commentaires:

Enregistrer un commentaire