实现一个检测系统发出的打印队列文件,打印的状态,打印机状态的模块。
先上效果图

要用到Win32_PrintJob,Win32_Printer。
其中,Win32_Printer是打印机的各种状态和控制。
Win32_Printer 类 - Win32 apps | Microsoft Learn
Win32_PrintJob是打印队列的各种数据和状态。Win32_PrintJob 类 - Win32 apps | Microsoft Learn
需要用到using System.Management; 这个库如果没有,就使用NuGet去获取

- //获取操作系统默认打印机名称
- public string GetDefPrinterName()
- {
- //获取所有打印机信息
- string query = string.Format("SELECT * from Win32_Printer ");
- var searcher = new ManagementObjectSearcher(query);
- var printers = searcher.Get();
- string strState = "";
- foreach (var printer in printers)
- {
- if (bool.Parse(printer.Properties["Default"].Value.ToString()) == true)
- {
- defPrintName = printer.Properties["DeviceID"].Value.ToString();
- }
- }
- return defPrintName;
- }
- public string GetPrintState()
- {
- //获取所有打印机信息
- string query = string.Format("SELECT * from Win32_Printer");
- var searcher = new ManagementObjectSearcher(query);
- var printers = searcher.Get();
- string strState = "";
- string defPrintName = "";
- foreach (var printer in printers)
- {
- if (bool.Parse(printer.Properties["Default"].Value.ToString()) == true)
- {
- int status = Int32.Parse(printer.Properties["ExtendedPrinterStatus"].Value.ToString());
- strState = ExtendedPrinterStatus[status - 1];
- break;
- }
- }
- return strState;
- }
这段代码写的不好,在select * from Win32_Printer这里是可以加上WHERE的。应该可以用 WHERE xxxName="Default"的方式去筛选出默认打印机,代码更简介。
- public List
GetPrintList() - {
- List
list = new List(); -
- var query2 = "SELECT * FROM Win32_PrintJob";
- using (var searcher2 = new ManagementObjectSearcher(query2))
- {
- var collection = searcher2.Get();
- if (collection.Count > 0)
- {
- foreach (ManagementObject item in collection)
- {
- string str = item.Properties["DriverName"].Value.ToString();
- if (defPrintName.Contains(item.Properties["DriverName"].Value.ToString()))
- {
- try
- {
- PrintJob job = new PrintJob();
- job.Document = (string)item.Properties["Document"].Value;
- job.JobId = Int32.Parse(item.Properties["JobId"].Value.ToString()).ToString();
- job.TotalPages = item.Properties["PaperSize"].Value.ToString();
- job.PagesPrinted = Int32.Parse(item.Properties["PagesPrinted"].Value.ToString()).ToString();
- job.TotalPages = Int32.Parse(item.Properties["TotalPages"].Value.ToString()).ToString();
- job.TimeSubmitted = item.Properties["TimeSubmitted"].Value.ToString();
- //job.StatusMask = GetStatusMaskCh(Int32.Parse(item.Properties["StatusMask"].Value.ToString()));
- if(item.Properties["JobStatus"].Value!=null)
- {
- job.JobStatus = item.Properties["JobStatus"].Value.ToString();
- }
- else
- {
- job.JobStatus = "";
- }
-
- list.Add(job);
- }
- catch (Exception ex)
- {
-
- }
-
- }
- }
- }
- }
-
- return list;
- }
这里的PrintJob是我自己创建的一个类,用来记录和传递我想要的状态的。请结合文章签名微软关于PrintJob的类的解释来看。我这里只是自己挑选了些我需要的出来。其余的均可采用上述代码中
item.Properties["Document"].Value
的方式来获取。我的PrintJob类构建如下:
- public class PrintJob
- {
- ///
- /// 文件名称
- ///
- public string Document { get; set; }
- ///
- /// 打印序列
- ///
- public string JobId { get; set; }
- ///
- /// 打印机返回的状态中文描述字符串
- ///
- public string JobStatus { get; set; }
- ///
- /// 页面大小
- ///
- public string PaperSize { get; set; }
- ///
- /// 已打印页数
- ///
- public string PagesPrinted { get; set; }
- ///
- /// 总页数
- ///
- public string TotalPages { get; set; }
- ///
- /// 提交时间
- ///
- public string TimeSubmitted { get; set; }
- ///
- /// 状态
- ///
- public string StatusMask { get; set; }
-
- }
特别说明下JobStatus ,这个获取的是打印机返回的状态文本描述。JobStatus是人最容易看懂的打印状态。而StatusMask、Status的状态更多是打印错误的状态,详情请阅读微软官方文档。
