如何确认是否选择了正确的打印机驱动端口选择端口

How can I detect what port (Ne01:, Ne02:, Ne99: etc) the printer is on?
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.
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.
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.
So far I have tried using the
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));
I also poked around in the System.Drawing.Printing class.
will give you the name of the printer "Adobe PDF" but I can't figure out how to get the port info.
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.
If I pass a hardcoded "Adobe PDF on Ne0x:" with an appropriate x value it works every time.
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)
Can anyone point me to code that correctly pulls the printer port?
解决方案 Here's what I ended up doing
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";
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 ).V
return printerName + " on " +
本文地址: &
我怎么能检测一下端口(Ne01 :, Ne02 :, Ne99:等)?打印机上
计算机(操作系统)在这里BigCorp安装Adobe Acrobat (7.0版专业版)安装这给命名为“的Adobe PDF”虚拟打印机。如果打印一个Excel(2003年)工作簿PDF录制宏,打印机的全名是“关于NEXX的Adobe PDF:”其中xx是一个两位数....和不同的取决于什么电脑你试试。
我写使用Excel.Interop(我强烈不鼓励任何人启动沿着这条道路地狱),打开了一系列的C#控制台应用程序电子表格。它运行在每一个宏,保存,打印是为PDF,然后移动PDF到共享驱动器上的报告文件夹中。
我所面临的问题是,每安装Acrobat似乎挑选的PDF打印机随机端口号......,我无法弄清楚如何得到它。
到目前为止,我已经尝试使用像这样
VAR搜索=新ManagementObjectSearcher(@“SELECT * FROM Win32_Printer”); 的foreach(的ManagementObject打印机searcher.Get()) {如果(Regex.IsMatch(打印机[“名称”]的ToString(),@。“(土坯| PDF)”, RegexOptions.IgnoreCase)) { //打印机[“名称”]; = GT; “的Adobe PDF” //打印机[“端口名称”] => “我的文档/ *。PDF”的foreach(在printer.Properties PropertyData PD) { Console.WriteLine(的String.Format(“{0},{1}”,PD。名称,pd.Value)); } 中断; } }
我也是在System.Drawing.Printing周围戳类。该会给你打印机“的Adobe PDF”的名字,但我无法弄清楚如何获得的端口的信息。
如果我通过公正“的Adobe PDF“它有时工作,有时对Excel互操作打印输出()方法失败,”文档打印失败“......我想不出为什么。
如果我通过一个硬编码“关于Ne0x的Adobe PDF:”与它的工作原理的每次的
如果我想尽一切可能的变化,Excel的一个适当的x值。有益打印到默认打印机。我没有更改默认的打印机(安全策略限制)的选项
任何人都可以点我到正确拉动打印机端口的代码?
解决方案 下面就是我最后做
使用的Microsoft.Win32;
无功设备= Registry.CurrentUser.OpenSubKey(@“Software\Microsoft\Windows NT\CurrentVersion\Devices”); //使用锁定的帐户串PRINTERNAME =“的Adobe PDF”,即使读访问;
试 { 的foreach(在devices.GetValueNames字符串名称()) {如果(Regex.IsMatch(姓名, PRINTERNAME,RegexOptions.IgnoreCase)) { VAR值=(字符串)devices.GetValue(名);
VAR端口= Regex.Match(值,@“(Ne\d + :)”,RegexOptions.IgnoreCase).value的; 返回PRINTERNAME +“上的”+端口; } } } 抓 {扔; }
本文地址: &
扫一扫关注官方微信我要分享到:
系统教程栏目
win7系统教程排行
12345678910
大家都在使用
win7系统推荐

我要回帖

更多关于 选择打印机端口 的文章

 

随机推荐