C# 打印机连接状态判断 _
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | /// <summary> /// 判断是否连接打印机 /// </summary> public bool CheckPrinter() { //取得默认打印机名 PrintDocument pdoc = new PrintDocument(); string printerName1 = pdoc.PrinterSettings.PrinterName; ManagementScope scope = new ManagementScope( @"\root\cimv2" ); scope.Connect(); // Select Printers from WMI Object Collections ManagementObjectSearcher searcher = new ManagementObjectSearcher( "SELECT * FROM Win32_Printer" ); string printerName = "" ; foreach (ManagementObject printer in searcher.Get()) { printerName = printer[ "Name" ].ToString().ToLower(); if (printerName.IndexOf(printerName1.ToLower()) > -1) { if (printer[ "WorkOffline" ].ToString().ToLower().Equals( "true" )) { switch (MessageBox.Show( "默认打印机未连接或出错" , "警告" , MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error)) { case DialogResult.Retry: CheckPrinter(); break ; } return false ; // printer is offline by user } else { // printer is not offline return true ; } } } switch (MessageBox.Show( "默认打印机未连接或出错" , "警告" , MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error)) { case DialogResult.Retry: CheckPrinter(); break ; } return false ; } /// <summary> /// 判断打印机状态,传Exception /// </summary> /// <param name="ex">Exception</param> public void ErrerPrint(Exception ex) { PrintQueue pq = LocalPrintServer.GetDefaultPrintQueue(); string PrintStaticText = "" ; bool isPrintErrer = false ; switch (pq.QueueStatus) { case PrintQueueStatus.Busy: PrintStaticText = "打印机正忙" ; isPrintErrer = true ; break ; case PrintQueueStatus.DoorOpen: PrintStaticText = "打印机上的门已打开" ; isPrintErrer = true ; break ; case PrintQueueStatus.Error: PrintStaticText = "由于错误情况,打印机无法打印。" ; isPrintErrer = true ; break ; case PrintQueueStatus.IOActive: PrintStaticText = "打印机正在与打印服务器交换数据" ; isPrintErrer = true ; break ; case PrintQueueStatus.Initializing: PrintStaticText = "打印机正在初始化" ; isPrintErrer = true ; break ; case PrintQueueStatus.ManualFeed: PrintStaticText = "打印机正在等待用户将打印介质放入手动送纸盒" ; isPrintErrer = true ; break ; case PrintQueueStatus.NoToner: PrintStaticText = "打印机墨粉用完" ; isPrintErrer = true ; break ; case PrintQueueStatus.None: PrintStaticText = "未指定状态。" ; isPrintErrer = true ; break ; case PrintQueueStatus.NotAvailable: PrintStaticText = "状态信息不可用" ; isPrintErrer = true ; break ; case PrintQueueStatus.Offline: PrintStaticText = "打印机处于脱机状态。" ; isPrintErrer = true ; break ; case PrintQueueStatus.OutOfMemory: PrintStaticText = "打印机无可用内存" ; isPrintErrer = true ; break ; case PrintQueueStatus.OutputBinFull: PrintStaticText = "打印机的输出纸盒已满" ; isPrintErrer = true ; break ; case PrintQueueStatus.PagePunt: PrintStaticText = "打印机不能打印当前页" ; isPrintErrer = true ; break ; case PrintQueueStatus.PaperJam: PrintStaticText = "打印机卡纸" ; isPrintErrer = true ; break ; case PrintQueueStatus.PaperOut: PrintStaticText = "打印机中没有或已用完当前打印作业所需的纸张类型" ; isPrintErrer = true ; break ; case PrintQueueStatus.PaperProblem: PrintStaticText = "打印机中的纸张导致未指定的错误情况" ; isPrintErrer = true ; break ; case PrintQueueStatus.Paused: PrintStaticText = "打印队列已暂停" ; isPrintErrer = true ; break ; case PrintQueueStatus.PendingDeletion: PrintStaticText = "打印队列正在删除打印作业" ; isPrintErrer = true ; break ; case PrintQueueStatus.PowerSave: PrintStaticText = "打印机处于节能模式" ; isPrintErrer = true ; break ; case PrintQueueStatus.Printing: PrintStaticText = "设备正在打印" ; isPrintErrer = true ; break ; case PrintQueueStatus.Processing: PrintStaticText = "设备正在执行某种工作,如果此设备是集打印机、传真机和扫描仪于一体的多功能设备,则不需要打印" ; isPrintErrer = true ; break ; case PrintQueueStatus.ServerUnknown: PrintStaticText = "打印机处于错误状态" ; isPrintErrer = true ; break ; case PrintQueueStatus.TonerLow: PrintStaticText = "打印机中只剩下少量墨粉" ; isPrintErrer = true ; break ; case PrintQueueStatus.UserIntervention: PrintStaticText = "打印机要求通过用户操作来更正错误情况" ; isPrintErrer = true ; break ; case PrintQueueStatus.Waiting: PrintStaticText = "打印机正在等待打印作业" ; isPrintErrer = true ; break ; case PrintQueueStatus.WarmingUp: PrintStaticText = "打印机正在预热" ; isPrintErrer = true ; break ; } if (isPrintErrer) { WriteLog.SetString(PrintStaticText); MessageBox.Show(PrintStaticText, "警告" , MessageBoxButtons.OK, MessageBoxIcon.Error); } if (ex.Message == "The net printer is unavailable." ) { MessageBox.Show( "网络打印机不可用" , "警告" , MessageBoxButtons.OK, MessageBoxIcon.Error); WriteLog.SetString(ex.Message); } else { WriteLog.SetException(ex); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)