如何在.NET 中获取打印机信息?
在标准 PrintDialog 中,有四个与所选打印机关联的值:Status、Type、Where 和 Comment。
如果我知道打印机的名称,如何在 C# 2.0 中获取这些值?
In the standard PrintDialog there are four values associated with a selected printer: Status, Type, Where, and Comment.
If I know a printer's name, how can I get these values in C# 2.0?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
正如 dowski 建议的,您可以使用 WMI 获取打印机属性。 以下代码显示给定打印机名称的所有属性。 其中您会发现:PrinterStatus、Comment、Location、DriverName、PortName 等
...
As dowski suggested, you could use WMI to get printer properties. The following code displays all properties for a given printer name. Among them you will find: PrinterStatus, Comment, Location, DriverName, PortName, etc.
...
这应该可以工作。
...
之后,各种 可以读取 PrinterSettings 的属性。
请注意,
ps.isValid()
可以查看打印机是否确实存在。编辑:一则补充评论。 Microsoft 建议您使用 PrintDocument 并修改其 PrinterSettings,而不是直接创建 PrinterSettings。
This should work.
...
After that, the various properties of PrinterSettings can be read.
Note that
ps.isValid()
can see if the printer actually exists.Edit: One additional comment. Microsoft recommends you use a PrintDocument and modify its PrinterSettings rather than creating a PrinterSettings directly.
查看 PrinterSettings.InstalledPrinters
Look at PrinterSettings.InstalledPrinters
仅供参考,此处 是打印机 ManagementObject 的所有可用属性的列表。
Just for reference, here is a list of all the available properties for a printer ManagementObject.
请注意 dowski 和 Panos 所引用的文章 (MSDN Win32_Printer)可能有点误导。
我指的是大多数数组的第一个值。 有的以1开头,有的以0开头。
例如,“ExtendedPrinterStatus”表中的第一个值是 1,因此,您的数组应该是这样的:
而另一方面,“ErrorState”表中的第一个值是 0,因此,你的数组应该是这样的:
顺便说一句,
“PrinterState”已过时,但您可以使用“PrinterStatus”。
Please notice that the article that dowski and Panos was reffering to (MSDN Win32_Printer) can be a little misleading.
I'm referring the first value of most of the arrays. some begins with 1 and some begins with 0.
for example, "ExtendedPrinterStatus" first value in table is 1, therefore, your array should be something like this:
and on the other hand, "ErrorState" first value in table is 0, therefore, your array should be something like this:
BTW,
"PrinterState" is obsolete, but you can use "PrinterStatus".
我已经很长时间没有在 Windows 环境中工作了,但我建议您查看 使用 WMI。
It's been a long time since I've worked in a Windows environment, but I would suggest that you look at using WMI.
我知道这是一个旧帖子,但现在更简单/更快的选择是使用 WPF 框架提供的增强打印服务(可由非 WPF 应用程序使用)。
http://msdn.microsoft.com/ en-us/library/System.Printing(v=vs.110).aspx
检索打印机队列和第一个作业的状态的示例。
I know it's an old posting, but nowadays the easier/quicker option is to use the enhanced printing services offered by the WPF framework (usable by non-WPF apps).
http://msdn.microsoft.com/en-us/library/System.Printing(v=vs.110).aspx
An example to retrieve the status of the printer queue and first job..
作为 WMI 的替代方案,您可以通过点击 WinSpool.drv(即 Windows API)来获得快速准确的结果 - 您可以获得接口、结构和结构的所有详细信息。 来自 pinvoke.net 的常量,或者我已将代码放在 http://delradiesdev.blogspot.com/2012/02/accessing-printer-status-using-winspool.html
As an alternative to WMI you can get fast accurate results by tapping in to WinSpool.drv (i.e. Windows API) - you can get all the details on the interfaces, structs & constants from pinvoke.net, or I've put the code together at http://delradiesdev.blogspot.com/2012/02/accessing-printer-status-using-winspool.html