Talking to a printer

前端 未结 2 2009
暖寄归人
暖寄归人 2020-12-30 12:33

Is there a way to write some code that can \'talk\' to printer in order to get some basic info about it\'s status? What I\'m really interested in doing is finding out if it

相关标签:
2条回答
  • 2020-12-30 12:56

    Getting information from Printers using System.Management is relatively easy.

        //Declare WMI Variables
        ManagementObject MgmtObject;
        ManagementObjectCollection MgmtCollection;
        ManagementObjectSearcher MgmtSearcher;
    
        //Perform the search for printers and return the listing as a collection
        MgmtSearcher = new ManagementObjectSearcher("Select * from Win32_Printer");
        MgmtCollection = MgmtSearcher.Get();
    
        foreach (ManagementObject objWMI in MgmtCollection)
        {
           //Do whatever action you want with the Printer
        }
    

    Look at http://msdn.microsoft.com/en-us/library/aa394363.aspx for methods and properties of Win32_Printer. For your question:

    //Test whether a Win32_Printer is out of paper or jammed
    int state = Int32.Parse(objWMI["PrinterState"]);
    if (state == 4) {
       //Paper Jam
    } else if (state == 5) {
       //Paper Out
    }
    
    0 讨论(0)
  • 2020-12-30 13:11

    You can use LINQ to WMI api too.

    0 讨论(0)
提交回复
热议问题