您好,登錄后才能下訂單哦!
這篇文章主要介紹“如何用c#實(shí)現(xiàn)打印機(jī)脫機(jī)檢查及阻塞打印”,在日常操作中,相信很多人在如何用c#實(shí)現(xiàn)打印機(jī)脫機(jī)檢查及阻塞打印問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何用c#實(shí)現(xiàn)打印機(jī)脫機(jī)檢查及阻塞打印”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
enum PrinterStatus { 其他 = 1, 未知 = 2, 空閑 = 3, 打印 = 4, 預(yù)熱 = 5, 停止打印 = 6, 脫機(jī) = 7, }/// <summary>/// 獲取打印機(jī)的當(dāng)前狀態(tài)/// </summary>/// <param name="PrinterDevice">打印機(jī)設(shè)備名稱</param>/// <returns>打印機(jī)狀態(tài)</returns>private static PrinterStatus GetPrinterPrinterStatus(string PrinterDevice) { PrinterStatus ret = 0;string path = @"win32_printer.DeviceId='" + PrinterDevice + "'"; ManagementObject printer = new ManagementObject(path); printer.Get(); ret = (PrinterStatus)Convert.ToInt32(printer.Properties["PrinterStatus"].Value);return ret; }
public static bool CheckPrinter(string BindPrintName) { ManagementScope scope = new ManagementScope(@"\root\cimv2"); scope.Connect();// Select Printers from WMI Object CollectionsManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");string printerName = "";foreach (ManagementObject printer in searcher.Get()) { printerName = printer["Name"].ToString().ToLower();if (printerName.IndexOf(BindPrintName.ToLower()) > -1) {//foreach (var property in printer.Properties)//{// LogUtil.WriteLog(property.Name + ":" + property.Value);//}if (printer["WorkOffline"].ToString().ToLower().Equals("true")) {return false; }else{return true; } } }return false; }
這里主要是通過(guò)一個(gè)while循環(huán)來(lái)不停的獲取打印機(jī)的狀態(tài),空閑時(shí)才會(huì)進(jìn)行下一次打印,通過(guò)兩個(gè)標(biāo)志位來(lái)配合輪詢的外部介入退出
FinishWait標(biāo)志位是任務(wù)內(nèi)部的標(biāo)識(shí),如果打印機(jī)檢查到了錯(cuò)誤狀態(tài)則進(jìn)行輪詢退出
GlobalInfo.IsGlobalExist標(biāo)志位是程序全局標(biāo)識(shí),如果界面上通知用戶等待時(shí)間過(guò)長(zhǎng),可以進(jìn)行人工手動(dòng)干預(yù)退出
//檢查打印機(jī)狀態(tài)if (!CheckPrinter(PrintName)) {throw new PrintCheckException("打印機(jī)處于脫機(jī)狀態(tài)"); } PrinterStatus State = GetPrinterPrinterStatus(PrintName);if (State == PrinterStatus.空閑) { }else if (State == PrinterStatus.打印 || State == PrinterStatus.預(yù)熱) {//正在打印/預(yù)熱時(shí)循環(huán)去查詢打印機(jī)狀態(tài),如果變成了就緒就開(kāi)始打印while (State != PrinterStatus.空閑 && !FinishWait && !GlobalInfo.IsGlobalExist) { State = GetPrinterPrinterStatus(PrintName);if (!CheckPrinter(PrintName)) { FinishWait = true;throw new PrintCheckException("打印機(jī)處于脫機(jī)狀態(tài)"); }if (State == PrinterStatus.打印 || State == PrinterStatus.預(yù)熱 || State == PrinterStatus.空閑) {if (State == PrinterStatus.空閑) { FinishWait = true; } }else{ FinishWait = true;throw new PrintCheckException("打印機(jī)狀態(tài)錯(cuò)誤,當(dāng)前狀態(tài):" + State.ToString()); } }
到此,關(guān)于“如何用c#實(shí)現(xiàn)打印機(jī)脫機(jī)檢查及阻塞打印”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。