在标准PrintDialog中,有四个与选定打印机关联的值:状态,类型,位置和注释.
如果我知道打印机的名称,我怎样才能在C#2.0中获得这些值?
正如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); } }
这应该工作.
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.
看看PrinterSettings.InstalledPrinters