PrintServerException - “...名称无效";即使我可以从 Windows 访问路径 [英] PrintServerException - "...name is invalid" even though I can access the path from windows

查看:13
本文介绍了PrintServerException - “...名称无效";即使我可以从 Windows 访问路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似下面的一行抛出了上面的异常:

A line similar to the following threw the above exception:

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

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

When I use "Run..." on Windows the address above does work and take me to the list of print jobs so why does the line of code not work?

推荐答案

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

Apparently, the address \prntsrv was a DNS alias to \prntserv and the PrintServer constructor was unable to deal with it. To get around this issue I used the following code (I could also use just the code in the catch block instead and it would work, but preferred not to):

        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;
            }
        }

这篇关于PrintServerException - “...名称无效";即使我可以从 Windows 访问路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆