小编典典

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

c#

在标准PrintDialog中,有四个与所选打印机关联的值:“状态”,“类型”,“位置”和“注释”。

如果我知道打印机的名称,如何在C#2.0中获得这些值?


阅读 326

收藏
2020-05-19

共1个答案

小编典典

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);
    }
}
2020-05-19