Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use c # to realize offline inspection and blocking printing of printer

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use c # to check offline and block printing". In daily operation, I believe many people have doubts about how to use c # to check offline and block printing. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to use c # to check offline printer and block printing". Next, please follow the editor to study!

Printer status gets enum PrinterStatus {other = 1, unknown = 2, idle = 3, print = 4, preheat = 5, stop printing = 6, offline = 7 } / get the current status of the printer / printer device name / printer status 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;} printer offline check 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;} Printer blocking queue implementation

Here is mainly through a while loop to constantly obtain the status of the printer, free time will be the next print, through two flag bits to cooperate with the external intervention of polling to exit

The FinishWait flag bit is the internal identity of the task. If the printer detects the error status, it will poll and exit.

The GlobalInfo.IsGlobalExist flag bit is the global identity of the program. If the user is notified on the interface that the waiting time is too long, manual intervention can be made to exit.

/ / check the printer status if (! CheckPrinter (PrintName)) {throw new PrintCheckException ("the printer is offline");} PrinterStatus State = GetPrinterPrinterStatus (PrintName); if (State = = PrinterStatus. Idle) {} else if (State = = PrinterStatus. Print | | State = = PrinterStatus. Preheat) {/ / printing / preheating cycle to query the printer status, if it becomes ready, start printing while (State! = PrinterStatus. Idle & &! FinishWait & &! GlobalInfo.IsGlobalExist) {State = GetPrinterPrinterStatus (PrintName); if (! CheckPrinter (PrintName)) {FinishWait = true;throw new PrintCheckException ("printer is offline");} if (State = = PrinterStatus. Print | | State = = PrinterStatus. Preheat | | State = = PrinterStatus. Idle) {if (State = = PrinterStatus. Idle) {FinishWait = true;}} else {FinishWait = true;throw new PrintCheckException ("Printer status error, current status:" + State.ToString ()) At this point, the study on "how to use c # to check offline and block printing" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report