如何在.NET 中获取打印机信息?

发布于 2024-07-08 20:33:20 字数 108 浏览 9 评论 0原文

在标准 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

小兔几 2024-07-15 20:33:21

正如 dowski 建议的,您可以使用 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);
    }
}

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.

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);
    }
}
仙女山的月亮 2024-07-15 20:33:21

应该可以工作。

using System.Drawing.Printing;

...

PrinterSettings ps = new PrinterSettings();
ps.PrinterName = "The printer name"; // Load the appropriate printer's setting

之后,各种 可以读取 PrinterSettings 的属性

请注意,ps.isValid() 可以查看打印机是否确实存在。

编辑:一则补充评论。 Microsoft 建议您使用 PrintDocument 并修改其 PrinterSettings,而不是直接创建 PrinterSettings。

This should work.

using System.Drawing.Printing;

...

PrinterSettings ps = new PrinterSettings();
ps.PrinterName = "The printer name"; // Load the appropriate printer's setting

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.

木森分化 2024-07-15 20:33:21

仅供参考,此处 是打印机 ManagementObject 的所有可用属性的列表。

usage: printer.Properties["PropName"].Value

Just for reference, here is a list of all the available properties for a printer ManagementObject.

usage: printer.Properties["PropName"].Value
何必那么矫情 2024-07-15 20:33:21

请注意 dowskiPanos 所引用的文章 (MSDN Win32_Printer)可能有点误导。

我指的是大多数数组的第一个值。 有的以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"
};

而另一方面,“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"
};

顺便说一句,
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:

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:

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".

千柳 2024-07-15 20:33:21

我已经很长时间没有在 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.

挽清梦 2024-07-15 20:33:21

我知道这是一个旧帖子,但现在更简单/更快的选择是使用 WPF 框架提供的增强打印服务(可由非 WPF 应用程序使用)。

http://msdn.microsoft.com/ en-us/library/System.Printing(v=vs.110).aspx

检索打印机队列和第一个作业的状态的示例。

var queue = new LocalPrintServer().GetPrintQueue("Printer Name");
var queueStatus = queue.QueueStatus;
var jobStatus = queue.GetPrintJobInfoCollection().FirstOrDefault().JobStatus

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..

var queue = new LocalPrintServer().GetPrintQueue("Printer Name");
var queueStatus = queue.QueueStatus;
var jobStatus = queue.GetPrintJobInfoCollection().FirstOrDefault().JobStatus
无人问我粥可暖 2024-07-15 20:33:21

作为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文