2009-05-20 81 views
7

有没有办法编写一些可以与打印机“交谈”的代码,以获取有关其状态的一些基本信息?我真正感兴趣的是找出它是否已经用完了纸张或是卡纸 - 这种性质的事情。我应该使用System.Management库来处理这种类型的东西吗?与打印机通话

PS - 知道如何获取已安装在特定PC上的所有打印机也很方便。你会怎么做?

+0

*戴上笑话帽*我父亲是打印机,我每天都跟他说话。 – 2009-05-20 13:16:46

回答

9

使用System.Management从打印机获取信息相对容易。

//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 
    } 

查看http://msdn.microsoft.com/en-us/library/aa394363.aspx了解Win32_Printer的方法和属性。对于您的问题:

//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 
}