如何确定什么“Ne”端口Adobe PDF打印机是否打开? [英] How to determine what "Ne" port the Adobe PDF printer is on?

查看:1680
本文介绍了如何确定什么“Ne”端口Adobe PDF打印机是否打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能检测一下端口(Ne01 :, Ne02 :, Ne99:等)?打印机上

How can I detect what port (Ne01:, Ne02:, Ne99: etc) the printer is on?

计算机(操作系统)在这里BigCorp安装Adobe Acrobat (7.0版专业版)安装这给命名为的Adobe PDF虚拟打印机。如果打印一个Excel(2003年)工作簿PDF录制宏,打印机的全名是关于NEXX的Adobe PDF:其中xx是一个两位数....和不同的取决于什么电脑你试试。

Computers (WinXP) here at BigCorp have Adobe Acrobat (version 7.0 Pro) installed which gives a virtual printer named "Adobe PDF". If you print an Excel (2003) workbook to pdf while recording a macro, the printer's full name is "Adobe PDF on Nexx:" where xx is a double digit.... and differs depending on what computer you try.

我写使用Excel.Interop(我强烈不鼓励任何人启动沿着这条道路地狱),打开了一系列的C#控制台应用程序电子表格。它运行在每一个宏,保存,打印是为PDF,然后移动PDF到共享驱动器上的报告文件夹中。

I have written a C# console app using the Excel.Interop (I strongly discourage anyone else from starting down this road to hell) that opens a series of spreadsheets. It runs a macro in each one, saves, prints is as a pdf, then moves the pdf to a reports folder on a shared drive.

我所面临的问题是,每安装Acrobat似乎挑选的PDF打印机随机端口号......,我无法弄清楚如何得到它。

The problem I face is that every install of Acrobat seems to pick a random port number for the PDF printer... and I can't figure out how to get it.

到目前为止,我已经尝试使用 Win32_Printer类像这样

So far I have tried using the Win32_Printer class like so

var searcher = new ManagementObjectSearcher( @"SELECT * FROM Win32_Printer" );
foreach ( ManagementObject printer in searcher.Get() )
{
   if ( Regex.IsMatch( printer["Name"].ToString(), @"(adobe|pdf)", RegexOptions.IgnoreCase ) )
   {
       //printer["Name"];    => "Adobe PDF"
       //printer["PortName"] => "my documents/*.pdf"
       foreach ( PropertyData pd in printer.Properties )
       {
           Console.WriteLine(string.Format("{0}, {1}", pd.Name, pd.Value));
       }
           break;
      }
}



我也是在System.Drawing.Printing周围戳类。该 PrinterSettings.InstalledPrinters 会给你打印机的Adobe PDF的名字,但我无法弄清楚如何获得的端口的信息。

I also poked around in the System.Drawing.Printing class. The PrinterSettings.InstalledPrinters will give you the name of the printer "Adobe PDF" but I can't figure out how to get the port info.

如果我通过公正的Adobe PDF它有时工作,有时对Excel互操作打印输出()方法失败,文档打印失败......我想不出为什么。

If I pass just "Adobe PDF" to the excel interop PrintOut() method it sometimes works and sometimes fails with "Document failed to print"... I cannot figure out why.

如果我通过一个硬编码关于Ne0x的Adobe PDF:与它的工作原理的每次

If I pass a hardcoded "Adobe PDF on Ne0x:" with an appropriate x value it works every time.

如果我想尽一切可能的变化,Excel的一个适当的x值。有益打印到默认打印机。我没有更改默认的打印机(安全策略限制)的选项

If I try every possible variation, Excel helpfully prints to the default printer. I do not have the option of changing the default printer (security policy restriction)

任何人都可以点我到正确拉动打印机端口的代码?

推荐答案

下面就是我最后做

using Microsoft.Win32;
...

        var devices = Registry.CurrentUser.OpenSubKey( @"Software\Microsoft\Windows NT\CurrentVersion\Devices" ); //Read-accessible even when using a locked-down account
        string printerName = "Adobe PDF";

        try
        {

            foreach ( string name in devices.GetValueNames() )
            {
                if ( Regex.IsMatch( name, printerName, RegexOptions.IgnoreCase ) )
                {
                    var value = (String)devices.GetValue( name );
                    var port = Regex.Match( value, @"(Ne\d+:)", RegexOptions.IgnoreCase ).Value;  
                    return printerName + " on " + port;
                }
            }
        }
        catch
        {
            throw;
        }

这篇关于如何确定什么“Ne”端口Adobe PDF打印机是否打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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