各位coder手下留情
/// <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 (printer["WorkOffline"].ToString().ToLower().Equals("true")) { switch (XtraMessageBox.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 (XtraMessageBox.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); XtraMessageBox.Show(PrintStaticText, "警告", MessageBoxButtons.OK, MessageBoxIcon.Error); } if (ex.Message == "The net printer is unavailable.") { XtraMessageBox.Show("网络打印机不可用", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error); WriteLog.SetString(ex.Message); } else { WriteLog.SetException(ex); } }