没有打印服务器的C#安装TCP / IP打印机不起作用

时间:2018-08-02 19:04:43

标签: c# wmi

我试图使用C#将打印机添加到HKEY_CURRENT_USER。

但是我遇到了错误“ Win32错误:打印名称不可接受”

有人可以帮助我吗?

我的代码在下面

string printerIP = "192.168.1.100";
            string portName = "IP_" + printerIP;
            string serverName = "Drucker";

            PrintServer ps = new PrintServer(serverName);

            ManagementClass printerPortClass = new ManagementClass("\\\\" + serverName + "\\root\\cimv2", "Win32_TCPIPPrinterPort", new ObjectGetOptions());
            printerPortClass.Get();
            var newPrinterPort = printerPortClass.CreateInstance();
            newPrinterPort.SetPropertyValue("Name", portName);
            newPrinterPort.SetPropertyValue("Protocol", 1);
            newPrinterPort.SetPropertyValue("HostAddress", printerIP);
            newPrinterPort.SetPropertyValue("PortNumber", 9100);
            newPrinterPort.SetPropertyValue("SNMPEnabled", false);
            newPrinterPort.Put();
            //install printer
            try
            {
                ps.InstallPrintQueue(
                    "PrinterName",
                    "DriverName",
                    new String[] { portName },    //port_name
                    "WinPrint",
                    PrintQueueAttributes.Shared,//set it to shared
                    "Sharename",
                    "This is a comment for the printer",
                    "This is the printers location",
                    "", 1, 1        //priorities
                    );
            }
            catch (Exception printerException)
            {
                //couldn't install printer
            }

0 个答案:

没有答案