C#以编程方式安装打印机驱动程序的最佳方法?

时间:2013-10-30 16:13:27

标签: c# networking printing driver

我正在将网络打印机添加到本地计算机上,我对如何将打印机驱动程序加载到计算机上感到很遗憾。我正在使用我在网上找到的一段代码,这使得添加打印机变得更加简单,但是使用驱动程序,您只需选择名称所需的驱动器即可。我尝试了一些使用printui.dll和命令提示符的方法,但每次尝试都失败了。我为每台打印机都有.INF文件。

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 个答案:

没有答案