PrintServerException - “...名称无效”,即使我可以从Windows访问路径

时间:2013-04-28 10:48:21

标签: c# dns print-spooler-api

类似于以下内容的行引发了上述异常:

PrintServer ps = new PrintServer(@"\\prntsrv");

当我使用"运行..."在Windows上,上面的地址确实有效,并将我带到打印作业列表中,为什么代码行不起作用?

2 个答案:

答案 0 :(得分:0)

显然,地址\\prntsrv\\prntserv的DNS别名,而PrintServer构造函数无法处理它。为了解决这个问题,我使用了下面的代码(我也可以只使用catch块中的代码,它可以工作,但不要使用):

        try
        {
            // Create object to monitor the printer queue
            PrintServer printServer = new PrintServer(serverPath);
            mPrintQueue = printServer.GetPrintQueue(printerName);
        }
        catch (PrintServerException ex)
        {
            // If the problem might be creating the server because the name is an alias
            if (ex.Message.Contains("printer name is invalid."))
            {
                string actualServerHostname = "\\\\" + Dns.GetHostEntry(serverPath.TrimStart('\\')).HostName;

                // Create object to monitor the printer queue
                PrintServer printServer = new PrintServer(actualServerHostname);
                mPrintQueue = printServer.GetPrintQueue(printerName);

                // Write to log about the use of a different address
            }
            else
            {
                throw;
            }
        }

答案 1 :(得分:0)

嘿,我正面临着类似的问题,这是我观察并做出的改变,只是试着让我知道。

由于Windows功能/角色"打印和文档服务"此问题正在发生。在系统上缺失。管理多台打印机或打印服务器以及将打印机迁移到其他Windows服务器或从其他Windows服务器迁移打印机时,需要此角色。

添加角色转到控制面板 - >打开或关闭Windows功能 - >单击复选框"打印和文档服务" - >安装。

如果您无法添加此规则,请与网络管理员联系以获取此规则。

添加角色后,您可以创建打印服务器对象并获取相应服务器上的所有打印队列。

相关问题