C# 如何在 .NET 中获取打印机信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/296182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get Printer Info in .NET?
提问by Nick Gotch
In the standard PrintDialog there are four values associated with a selected printer: Status, Type, Where, and Comment.
在标准 PrintDialog 中,有四个与选定打印机相关联的值:状态、类型、位置和注释。
If I know a printer's name, how can I get these values in C# 2.0?
如果我知道打印机的名称,如何在 C# 2.0 中获得这些值?
采纳答案by Panos
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.
正如多夫斯基建议的那样,您可以使用 WMI 来获取打印机属性。以下代码显示给定打印机名称的所有属性。其中您会发现:PrinterStatus、Comment、Location、DriverName、PortName 等。
using System.Management;
...
...
string printerName = "YourPrinterName";
string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
using (ManagementObjectCollection coll = searcher.Get())
{
try
{
foreach (ManagementObject printer in coll)
{
foreach (PropertyData property in printer.Properties)
{
Console.WriteLine(string.Format("{0}: {1}", property.Name, property.Value));
}
}
}
catch (ManagementException ex)
{
Console.WriteLine(ex.Message);
}
}
回答by dowski
回答by John Sheehan
回答by Powerlord
This shouldwork.
这应该有效。
using System.Drawing.Printing;
...
...
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = "The printer name"; // Load the appropriate printer's setting
After that, the various propertiesof PrinterSettings can be read.
之后,可以读取PrinterSettings的各种属性。
Note that ps.isValid()
can see if the printer actually exists.
注意ps.isValid()
可以查看打印机是否实际存在。
Edit: One additional comment. Microsoft recommends you use a PrintDocumentand modify its PrinterSettings rather than creating a PrinterSettings directly.
编辑:一个额外的评论。Microsoft 建议您使用PrintDocument并修改其 PrinterSettings,而不是直接创建 PrinterSettings。
回答by itsho
Please notice that the article that dowskiand Panoswas reffering to (MSDN Win32_Printer) can be a little misleading.
请注意,dowski和Panos所指的文章( MSDN Win32_Printer) 可能有点误导。
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:
我指的是大多数数组的第一个值。有些以 1 开头,有些以 0 开头。例如,“ ExtendedPrinterStatus”表中的第一个值是 1,因此,您的数组应该是这样的:
string[] arrExtendedPrinterStatus = {
"","Other", "Unknown", "Idle", "Printing", "Warming Up",
"Stopped Printing", "Offline", "Paused", "Error", "Busy",
"Not Available", "Waiting", "Processing", "Initialization",
"Power Save", "Pending Deletion", "I/O Active", "Manual Feed"
};
and on the other hand, "ErrorState" first value in table is 0, therefore, your array should be something like this:
另一方面,“ ErrorState”表中的第一个值是 0,因此,您的数组应该是这样的:
string[] arrErrorState = {
"Unknown", "Other", "No Error", "Low Paper", "No Paper", "Low Toner",
"No Toner", "Door Open", "Jammed", "Offline", "Service Requested",
"Output Bin Full"
};
BTW, "PrinterState" is obsolete, but you can use "PrinterStatus".
顺便说一句,“ PrinterState”已过时,但您可以使用“ PrinterStatus”。
回答by David
回答by MarkMiddlemist
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
作为 WMI 的替代方案,您可以通过进入 WinSpool.drv(即 Windows API)获得快速准确的结果 - 您可以从 pinvoke.net 获取有关接口、结构和常量的所有详细信息,或者我已将代码放在一起在http://delradiesdev.blogspot.com/2012/02/accessing-printer-status-using-winspool.html
回答by stoj
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).
我知道这是一个旧帖子,但现在更简单/更快的选择是使用 WPF 框架提供的增强打印服务(可由非 WPF 应用程序使用)。
http://msdn.microsoft.com/en-us/library/System.Printing(v=vs.110).aspx
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..
检索打印机队列和第一个作业状态的示例..
var queue = new LocalPrintServer().GetPrintQueue("Printer Name");
var queueStatus = queue.QueueStatus;
var jobStatus = queue.GetPrintJobInfoCollection().FirstOrDefault().JobStatus